gpt4 book ai didi

WPF 数据网格 : Binding a Collection Property to Column

转载 作者:行者123 更新时间:2023-12-04 07:22:15 25 4
gpt4 key购买 nike

我有一个名为 Person 的类:

public class Person
{
private string _Name;
private ObservableCollection<Smartphone> _Smartphones;

// Properties
}

public class Smartphone
{
private string _Manufacturer;
private bool _IsWorking;

// Properties
}

在我的 View 中,我有一个 DataGrid。
我的问题是:

有没有办法让我的 DataGrid 看起来像这样:

enter image description here

所有的人都在他们的收藏中拥有相同的智能手机,但具有不同的值(value)观或“IsWorking”......

编辑:
我已经在 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>

但这不是我想要的“解决方案”......

编辑 2 :如果可以有一个复选框而不是“真/假”,那就太好了:) ...

最佳答案

尝试这个

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

Edit If you want Dynamically try this

Custom DataGrid and Converter


    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


    <StackPanel>
<local:MyDataGrid AutoGenerateColumns="False"
ColumnHeaders="{Binding ColumnHeaders}"
ItemsSource="{Binding PersonCollection}"
CanUserAddRows="False" IsReadOnly="True">

</local:MyDataGrid>
</StackPanel>

xaml.cs


    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



Output

I hope this will help.

关于WPF 数据网格 : Binding a Collection Property to Column,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25266821/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com