2013-02-01 34 views
5

Trong Wpf, tôi có thể tạo một dòng dữ liệu có hàng khác nhau chứa loại điều khiển khác nhau trong cùng một cột không?Cột Datagrid có thể chứa loại điều khiển khác nhau ở hàng khác nhau

Đối với trường hợp đơn giản nhất: DataGrid với 5 màu sắc, 2 hàng, không quan tâm đến 4 cols đầu tiên, trong col 5:

  • hàng 1: đó là một hộp
  • thứ 2 liên tiếp: đó là một combobox .

Cảm ơn!

Trả lời

6

Bạn có thể sử dụng một DataGridTemplateColumn kết hợp với một vài trigger để đạt được chức năng này.

Đây là một ứng dụng demo liên kết một DataGrid với một danh sách các loại điều khiển (chuỗi). Cột đầu tiên chỉ hiển thị chuỗi loại điều khiển và cột thứ hai hoạt động trên cùng một thông tin để trình bày Điều khiển tương ứng. Bạn có thể có thể làm cho XAML một chút ngắn gọn hơn, nhưng đây là jist của nó:

Các XAML:

<Window x:Class="DataGridWithMultipleTypesPerColumn.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="MainWindow" Height="350" Width="525"> 
<Grid> 
    <DataGrid ItemsSource="{Binding ControlTypes}" 
       AutoGenerateColumns="False"> 
     <DataGrid.Columns> 
     <DataGridTextColumn Header="Control Type" Binding="{Binding}"/> 
      <DataGridTemplateColumn Header="Actual Control"> 
       <DataGridTemplateColumn.CellTemplate> 
        <DataTemplate> 
         <ContentControl> 
          <ContentControl.Style> 
           <Style TargetType="ContentControl"> 
            <Style.Triggers> 
             <DataTrigger Binding="{Binding}" Value="TextBox"> 
              <Setter Property="ContentTemplate"> 
               <Setter.Value> 
                <DataTemplate> 
                 <TextBox Text="Default Text"/> 
                </DataTemplate> 
               </Setter.Value> 
              </Setter> 
             </DataTrigger> 
             <DataTrigger Binding="{Binding}" Value="CheckBox"> 
              <Setter Property="ContentTemplate"> 
               <Setter.Value> 
                <DataTemplate> 
                 <CheckBox Content="Check Box"/> 
                </DataTemplate> 
               </Setter.Value> 
              </Setter> 
             </DataTrigger> 
             <DataTrigger Binding="{Binding}" Value="Button"> 
              <Setter Property="ContentTemplate"> 
               <Setter.Value> 
                <DataTemplate> 
                 <Button Content="Button"/> 
                </DataTemplate> 
               </Setter.Value> 
              </Setter> 
             </DataTrigger> 
            </Style.Triggers> 
           </Style> 
          </ContentControl.Style> 
         </ContentControl> 
        </DataTemplate> 
       </DataGridTemplateColumn.CellTemplate> 
      </DataGridTemplateColumn> 
     </DataGrid.Columns> 
    </DataGrid> 
</Grid> 

Mã-đằng sau và Xem mẫu:

namespace DataGridWithMultipleTypesPerColumn 
{ 
    public partial class MainWindow : Window 
    { 
     public MainWindow() 
     { 
      InitializeComponent(); 
      this.DataContext = new ViewModel(); 
     } 
    } 

    public class ViewModel 
    { 
     public ObservableCollection<string> ControlTypes 
     { 
      get; 
      private set; 
     } 
     public ViewModel() 
     { 
      ControlTypes = new ObservableCollection<string>() { "Button", "TextBox", "CheckBox" }; 
     } 
    } 
} 
+1

cảm ơn anh chàng, tôi có thể làm ngay bây giờ. Tôi tìm thấy một hướng dẫn khá dễ hiểu: http://code.msdn.microsoft.com/mag201104DataPoints. Tôi chưa thử hướng dẫn của bạn, nhưng nó có vẻ hợp lý. – kidgu

+0

Làm thế nào điều này sẽ làm việc với dữ liệu ràng buộc để kiểm soát chúng tôi đang tự động lựa chọn? – bgura

+1

Làm cách nào để chúng tôi có thể thực hiện công việc ràng buộc trong trường hợp này? Ví dụ, bạn có điều này: , nhưng làm thế nào để chúng ta ràng buộc văn bản bất động sản để một cái gì đó? Vấn đề là TextBox này không có DataContext và không nhìn thấy datacontext của cha mẹ. – nightcoder

Các vấn đề liên quan