WPF DataGrid:将集合属性绑定到列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25266821/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF DataGrid: Binding a Collection Property to Column
提问by Kajetan Jauk
I have a Class called Person:
我有一个名为 Person 的类:
public class Person
{
private string _Name;
private ObservableCollection<Smartphone> _Smartphones;
// Properties
}
public class Smartphone
{
private string _Manufacturer;
private bool _IsWorking;
// Properties
}
And in my View I have a DataGrid. My Question is:
在我的视图中,我有一个 DataGrid。我的问题是:
Is there a way to make my DataGrid look like this:
有没有办法让我的 DataGrid 看起来像这样:


All Persons have the same Smartphones in their Collection, but with different values vor "IsWorking"...
所有的人都在他们的收藏中拥有相同的智能手机,但具有不同的价值观 vor “IsWorking”...
EDIT:I've tried it with a DataGrid in a DataGrid like:
编辑:我已经在 DataGrid 中使用 DataGrid 进行了尝试,例如:
<DataGrid ItemsSource="{Binding PersonCollection}"
AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Name}" Header="Name" />
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<DataGrid ItemsSource="{Binding Smartphones}">
</DataGrid>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
But it is not the "solution" I would like to have...
但这不是我想要的“解决方案”......
EDIT 2: It would be nice, if there could be a Checkbox instead of "true/false" :) ...
编辑2:如果可以有一个复选框而不是“真/假”,那就太好了:) ...
回答by yo chauhan
Try this
尝试这个
SmartPhone.cs Person.cs
SmartPhone.cs Person.cs
public class Person
{
public string Name { get; set; }
public ObservableCollection<SmartPhone> SmartPhones { get; set; }
}
public class SmartPhone
{
public string Manufacturer { get; set; }
public bool IsWorking { get; set; }
}
EditIf you want Dynamically try this
Custom DataGrid and Converter
编辑如果你想动态试试这个
自定义 DataGrid 和转换器
public class MyDataGrid:DataGrid
{
public ObservableCollection<string> ColumnHeaders
{
get { return GetValue(ColumnHeadersProperty) as ObservableCollection<string>; }
set { SetValue(ColumnHeadersProperty, value); }
}
public static readOnly DependencyProperty ColumnHeadersProperty = DependencyProperty.Register("ColumnHeaders", typeof(ObservableCollection<string>), typeof(MyDataGrid), new PropertyMetadata(new PropertyChangedCallback(OnColumnsChanged)));
static void OnColumnsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var dataGrid=d as MyDataGrid;
dataGrid.Columns.Clear();
//Add Person Column
dataGrid.Columns.Add(new DataGridTextColumn() { Header = "Name", Binding = new Binding("Name") });
//Add Manufactures Columns
foreach (var value in dataGrid.ColumnHeaders)
{
var column=new DataGridCheckBoxColumn(){Header=value,Binding=new Binding("SmartPhones"){ConverterParameter=value,Converter=new ManufacturerConverter()}};
dataGrid.Columns.Add(column);
}
}
}
public class ManufacturerConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var smartPhones = value as IEnumerable<SmartPhone>;
if (smartPhones != null && parameter!=null)
{
var phone=smartPhones.FirstOrDefault(s => s.Manufacturer == parameter.ToString());
if (phone != null)
return phone.IsWorking;
return false;
}
return false;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
xaml
xml
<StackPanel>
<local:MyDataGrid AutoGenerateColumns="False"
ColumnHeaders="{Binding ColumnHeaders}"
ItemsSource="{Binding PersonCollection}"
CanUserAddRows="False" IsReadOnly="True">
</local:MyDataGrid>
</StackPanel>
xaml.cs
xml文件
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = new ViewModel();
}
}
ViewModel
视图模型
public class ViewModel
{
public ViewModel()
{
ColumnHeaders = new ObservableCollection<string>();
PersonCollection = new ObservableCollection<Person>()
{
new Person(){Name="Foo",
SmartPhones=new ObservableCollection<SmartPhone>()
{new SmartPhone(){Manufacturer="Manufacturer1",IsWorking=true}
,new SmartPhone(){Manufacturer="Manufacturer2",IsWorking=false}}}
, new Person(){Name="Bar",
SmartPhones=new ObservableCollection<SmartPhone>()
{new SmartPhone(){Manufacturer="Manufacturer1",IsWorking=true}
,new SmartPhone(){Manufacturer="Manufacturer2",IsWorking=false}
,new SmartPhone(){Manufacturer="Manufacturer3",IsWorking=true}}}
, new Person(){Name="FooBar",
SmartPhones=new ObservableCollection<SmartPhone>()
{new SmartPhone(){Manufacturer="Manufacturer1",IsWorking=true}
,new SmartPhone(){Manufacturer="Manufacturer2",IsWorking=false}
,new SmartPhone(){Manufacturer="Manufacturer3",IsWorking=true}
,new SmartPhone(){Manufacturer="Manufacturer4",IsWorking=false}
,new SmartPhone(){Manufacturer="Manufacturer5",IsWorking=true}
}}
};
foreach (var item in PersonCollection.SelectMany(s=>s.SmartPhones).Select(s=>s.Manufacturer).Distinct())
{
ColumnHeaders.Add(item);
}
}
public ObservableCollection<Person> PersonCollection { get; set; }
public ObservableCollection<string> ColumnHeaders { get; set; }
}
Output
输出


I hope this will help.
我希望这将有所帮助。

