2016-12-15 6 views
0

異なるセル内容の異なるデータグリッドに異なる行を表示しようとしています。WPFデータグリッドの列に異なる種類のデータを保持することができます

Iは、例えば

クラス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)); 

..私は上の3番目の列に空のテキストボックスを表示するためのデータグリッドをご希望の意味2番目の行には、最初の行にチェックボックスがあり、3番目の行には何もありません。これは可能ですか?ちょうどあなたのGridResourcesに追加

+0

確かに可能で、難易度は固定列か動的かどうかによって異なります。 –

答えて

1

あなたはこの使用DataTemplatesを行うことができます。ここ

<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; 
    } 
} 

すべてだ、私はあなたがより多くの例が必要になります場合はお知らせください。

よろしくお願いいたします。

関連する問題