2016-12-15 23 views
0

我試圖在有時用不同的cellcontent在datagrid中顯示不同的行。WPF數據網格中的列是否可以保存不同類型的數據

我有不同的行不同的類,例如

1類:

名稱 - 說明 - 複選框

2類:

名稱 - 說明 - 文本框(用戶輸入 - 複選框

3級

名稱 - 文本框(運行時用戶輸入)

這些類是通過繼承關聯的,所以我可以在同一個observablecollection中使用它們。

我想基於我選擇來添加,例如哪個類的數據網格顯示這些:

ObservableCollection<Rowitem> rowitems = new ObservableCollection<Rowitem>(); 

rowitems.Add(new Class1("Tom", "Nice", false)); 

rowitems.Add(new Class2("John", "Strange", Empty textbox , true)); 

rowitems.Add(new Class3("Roger", Empty Textbox)); 

..意思我想數據網格中的第三列顯示一個空文本框第二行,第一行有複選框,第三行沒有。這可能嗎?

+0

當然可能,難點取決於你想要固定列還是動態列。 –

回答

1

你可以做到這一點使用的DataTemplates:

只需將它們添加到您的GridResources

<Grid.Resources> 
     <DataTemplate DataType="{x:Type local:Class1}"> 
      <-- Template for class1 --> 
     </DataTemplate> 
     <DataTemplate DataType="{x:Type local:Class2}"> 
      <-- Template for class2 --> 
     </DataTemplate> 
</Grid.Resources> 
0

這裏是我的建議:

<Window x:Class="DataGridDynamicCellView.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:local="clr-namespace:DataGridDynamicCellView" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    Title="MainWindow" 
    Width="525" 
    Height="350" 
    mc:Ignorable="d"> 
<Window.DataContext> 
    <local:DynamicCellsDataContext /> 
</Window.DataContext> 
<Grid> 
    <DataGrid ItemsSource="{Binding DataGridSource}"> 
     <DataGrid.Resources> 
      <DataTemplate DataType="{x:Type local:PresentedByCheckBox}"> 
       <Grid HorizontalAlignment="Stretch" 
         VerticalAlignment="Stretch"> 
        <CheckBox HorizontalAlignment="Center" 
           VerticalAlignment="Center" 
           IsChecked="{Binding IsChecked, 
                UpdateSourceTrigger=PropertyChanged}" /> 
       </Grid> 
      </DataTemplate> 
      <DataTemplate DataType="{x:Type local:PresentedByTextBox}"> 
       <Grid HorizontalAlignment="Stretch" 
         VerticalAlignment="Stretch"> 
        <TextBlock HorizontalAlignment="Center" 
           VerticalAlignment="Center" 
           Text="{Binding HelloWorld, 
               UpdateSourceTrigger=PropertyChanged}" /> 
       </Grid> 
      </DataTemplate> 
      <DataTemplate DataType="{x:Type local:PresentedByComplexBox}"> 
       <StackPanel HorizontalAlignment="Stretch" 
          VerticalAlignment="Stretch" 
          Orientation="Horizontal"> 
        <Ellipse Height="10" Width="10" Fill="Pink"/> 
        <CheckBox HorizontalAlignment="Center" 
           VerticalAlignment="Center" 
           IsChecked="{Binding Checked, 
                UpdateSourceTrigger=PropertyChanged}" /> 
        <TextBlock HorizontalAlignment="Center" 
           VerticalAlignment="Center" 
           Text="{Binding HelloWorld, 
               UpdateSourceTrigger=PropertyChanged}" /> 
       </StackPanel> 
      </DataTemplate> 
      <Style TargetType="{x:Type DataGridCell}"> 
       <Setter Property="BorderBrush" Value="Green" /> 
       <Setter Property="BorderThickness" Value="2" /> 
       <Setter Property="ContentTemplate"> 
        <Setter.Value> 
         <DataTemplate> 
          <ContentControl Content="{Binding}" /> 
         </DataTemplate> 
        </Setter.Value> 
       </Setter> 
      </Style> 
     </DataGrid.Resources> 
    </DataGrid> 
</Grid></Window> 

MVVM視圖模型:

public class DynamicCellsDataContext:BaseObservableObject 
{ 
    public DynamicCellsDataContext() 
    { 
     DataGridSource = new ObservableCollection<object> 
     { 
      new PresentedByTextBox("Hello world!!!"), 
      new PresentedByCheckBox(true), 
      new PresentedByComplexBox("Hello world!!!", true), 
     }; 
    } 
    public ObservableCollection<object> DataGridSource { get; set; } 
} 

public class PresentedByComplexBox:BaseObservableObject 
{ 
    private string _helloWorld; 
    private bool _checked; 

    public string HelloWorld  
    { 
     get { return _helloWorld; } 
     set 
     { 
      _helloWorld = value; 
      OnPropertyChanged(); 
     } 
    } 

    public bool Checked 
    { 
     get { return _checked; } 
     set 
     { 
      _checked = value; 
      OnPropertyChanged(); 
     } 
    } 

    public PresentedByComplexBox(string helloWorld, bool isChecked) 
    { 
     HelloWorld = helloWorld; 
     Checked = isChecked; 
    } 
} 

public class PresentedByCheckBox:BaseObservableObject 
{ 
    private bool _isChecked; 

    public bool IsChecked 
    { 
     get { return _isChecked; } 
     set 
     { 
      _isChecked = value; 
      OnPropertyChanged(); 
     } 
    } 

    public PresentedByCheckBox(bool isChecked) 
    { 
     IsChecked = isChecked; 
    } 
} 

public class PresentedByTextBox:BaseObservableObject 
{ 
    private string _helloWorld; 

    public string HelloWorld 
    { 
     get { return _helloWorld; } 
     set 
     { 
      _helloWorld = value; 
      OnPropertyChanged(); 
     } 
    } 

    public PresentedByTextBox(string helloWorld) 
    { 
     HelloWorld = helloWorld; 
    } 
} 

的BaseObservableObject類:

public class BaseObservableObject : INotifyPropertyChanged 
{ 
    public event PropertyChangedEventHandler PropertyChanged; 

    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) 
    { 
     var handler = PropertyChanged; 
     if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName)); 
    } 

    protected virtual void OnPropertyChanged<T>(Expression<Func<T>> raiser) 
    { 
     var propName = ((MemberExpression)raiser.Body).Member.Name; 
     OnPropertyChanged(propName); 
    } 

    protected bool Set<T>(ref T field, T value, [CallerMemberName] string name = null) 
    { 
     if (!EqualityComparer<T>.Default.Equals(field, value)) 
     { 
      field = value; 
      OnPropertyChanged(name); 
      return true; 
     } 
     return false; 
    } 
} 

這一切,讓我知道如果你需要更多的例子。

此致敬禮。

相關問題