C# WPF datagrid中的列可以保存不同类型的数据吗
有时,我试图在数据网格中用不同的cellcontent显示不同的行 例如,对于不同的行,我有不同的类 第1类: 名称-说明-复选框 第2类: 名称-说明-文本框(运行时用户输入)-复选框 第三类 名称-文本框(运行时的用户输入) 这些类通过继承相互关联,因此我可以在同一个ObservaleCollection中使用它们 我想根据我选择添加的类在datagrid中显示这些,例如:C# WPF datagrid中的列可以保存不同类型的数据吗,c#,wpf,datagrid,C#,Wpf,Datagrid,有时,我试图在数据网格中用不同的cellcontent显示不同的行 例如,对于不同的行,我有不同的类 第1类: 名称-说明-复选框 第2类: 名称-说明-文本框(运行时用户输入)-复选框 第三类 名称-文本框(运行时的用户输入) 这些类通过继承相互关联,因此我可以在同一个ObservaleCollection中使用它们 我想根据我选择添加的类在datagrid中显示这些,例如: ObservableCollection<Rowitem> rowitems = new Observab
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));
ObservableCollection行项目=新的ObservableCollection();
添加(新的类别1(“Tom”,“Nice”,false));
添加(新类2(“John”,“奇怪”,空文本框,true));
添加(新类3(“罗杰”,空文本框));
。。这意味着我希望datagrid在第二行的第三列中显示一个空文本框,其中第一行中有一个复选框,第三行中没有任何内容。这可能吗?您可以使用数据模板执行此操作: 只需将它们添加到
网格的资源中
:
<Grid.Resources>
<DataTemplate DataType="{x:Type local:Class1}">
<-- Template for class1 -->
</DataTemplate>
<DataTemplate DataType="{x:Type local:Class2}">
<-- Template for class2 -->
</DataTemplate>
</Grid.Resources>
以下是我的建议:
<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;
}
}
公共类DynamicCellsDataContext:BaseObserveObject
{
公共动态CellsDataContext()
{
DataGridSource=新的ObservableCollection
{
新的PresentedByTextBox(“你好,世界!!!”),
新呈现的ByCheckBox(真),
新的PresentedByComplexBox(“你好,世界!!!”,正确),
};
}
公共ObservableCollection DataGridSource{get;set;}
}
public类PresentedByComplexBox:BaseObserveObject
{
私有字符串_helloWorld;
检查私人布尔;
公共字符串HelloWorld
{
获取{return\u helloWorld;}
设置
{
_helloWorld=值;
OnPropertyChanged();
}
}
公共图书馆检查
{
获取{return\u checked;}
设置
{
_选中=值;
OnPropertyChanged();
}
}
public PresentedByComplexBox(字符串helloWorld,布尔已选中)
{
HelloWorld=HelloWorld;
已检查=已检查;
}
}
由checkbox呈现的公共类:BaseObserveObject
{
检查私人住宅;
公共场所被检查
{
获取{return\u已检查;}
设置
{
_isChecked=值;
OnPropertyChanged();
}
}
公共演示的检查框(布尔已检查)
{
IsChecked=IsChecked;
}
}
公共类PresentedByTextBox:BaseObserveObject
{
私有字符串_helloWorld;
公共字符串HelloWorld
{
获取{return\u helloWorld;}
设置
{
_helloWorld=值;
OnPropertyChanged();
}
}
公共PresentedByTextBox(字符串helloWorld)
{
HelloWorld=HelloWorld;
}
}
BaseObserveObject类:
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;
}
}
公共类BaseObserveObject:INotifyPropertyChanged
{
公共事件属性更改事件处理程序属性更改;
受保护的虚拟void OnPropertyChanged([CallerMemberName]字符串propertyName=null)
{
var handler=PropertyChanged;
if(handler!=null)handler(这是新的PropertyChangedEventArgs(propertyName));
}
受保护的虚拟void OnPropertyChanged(表达式提升器)
{
var propName=((MemberExpression)raiser.Body).Member.Name;
OnPropertyChanged(propName);
}
受保护的布尔集合(参考T字段,T值,[CallerMemberName]字符串名称=null)
{
如果(!EqualityComparer.Default.Equals(字段,值))
{
字段=值;
OnPropertyChanged(名称);
返回true;
}
返回false;
}
}
就这些,如果你需要更多的例子,请告诉我
致以最诚挚的问候。当然可能,难度取决于您是想要固定列还是动态列。