【发布时间】:2017-05-14 15:45:00
【问题描述】:
我想将 DataGrid 绑定到嵌套属性的集合。
我尝试了来自
WPF: Bound datagrid does not update items properties 的解决方案
但到目前为止没有运气。
我得到的是一个空的 DataGrid 和这样的控制台输出:
System.Windows.Data 错误:2:找不到目标元素的管理 FrameworkElement 或 FrameworkContentElement。绑定表达式:路径=AGNR.Key;数据项=空;目标元素是“DataGridTextColumn”(HashCode=38805039);目标属性是“标题”(类型“对象”)
也是 tcc_CollectionChanged 的输出(但不是 PropertyChangedHandler):
30.12.2016 11:11:22,收藏已更改
我使用现代 UI(一楼)框架。这是我的窗口代码:
public partial class Home : UserControl
{
private ObservableTable<TableClass> tcc;
public Home()
{
InitializeComponent();
}
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
TableClass tc;
List<TableClass> tcl = new List<TableClass>();
tcc = new ObservableTable<TableClass>();
tcc.CollectionChanged += tcc_CollectionChanged;
tcc.ItemPropertyChanged += PropertyChangedHandler;
for (int i = 0; i < 10; i++)
{
tc = new TableClass();
tc.AGNR.Name = "AGNr";
tc.AGNR.Value = i.ToString();
tc.MNR.Name = "MNr";
tc.MNR.Value = i.ToString() + " M";
tc.MST.Name = "MSt";
tc.MST.Value = i % 2 == 0 ? "production" : "stopped";
tcc.Add(tc);
}
}
static void PropertyChangedHandler(object sender, PropertyChangedEventArgs e)
{
Console.WriteLine(DateTime.Now.ToString() + ", Property changed");
return;
}
static void tcc_CollectionChanged(object sender, EventArgs e)
{
Console.WriteLine(DateTime.Now.ToString() + ", Collection changed");
return;
}
}
对应的 XAML:
<UserControl x:Class="MuiWpfTestApp.Pages.Home"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300"
Loaded="UserControl_Loaded">
<Grid Style="{StaticResource ContentRoot}">
<ScrollViewer>
<DataGrid x:Name="tcgrid" ItemsSource="{Binding tcc, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Height="250" Width="250">
<DataGridTextColumn Binding="{Binding AGNR.Value, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"
Header="{Binding AGNR.Key, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"/>
<DataGridTextColumn Binding="{Binding MNR.Value, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"
Header="{Binding MNR.Key, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"/>
<DataGridTextColumn Binding="{Binding MST.Value, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"
Header="{Binding MST.Key, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"/>
</DataGrid>
</ScrollViewer>
</Grid>
INotifyPropertyChanged 在 TableClass 中实现:
class TableClass : INotifyPropertyChanged
{
private KeyValueClass _mnr;
private KeyValueClass _agnr;
private KeyValueClass _mst;
public KeyValueClass MNR
{
get
{
return _mnr;
}
set
{
_mnr = value;
NotifyPropertyChanged("MNR");
}
}
public KeyValueClass AGNR
{
get
{
return _agnr;
}
set
{
_agnr = value;
NotifyPropertyChanged("AGNR");
}
}
public KeyValueClass MST
{
get
{
return _mst;
}
set
{
_mst = value;
NotifyPropertyChanged("MST");
}
}
public TableClass()
{
MNR = new KeyValueClass();
AGNR = new KeyValueClass();
MST = new KeyValueClass();
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String propertyName = "")
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
而且 KeyValueClass 很简单(这里需要什么?):
class KeyValueClass
{
private string _name;
private string _val;
public string Name
{
get
{
return _name;
}
set
{
_name = value;
}
}
public string Value
{
get
{
return _val;
}
set
{
_val = value;
}
}
}
我需要集合中的这个嵌套属性,因为我可以用不同的语言获取这些数据。所以我无法对网格的标题进行编码。
【问题讨论】:
标签: c# wpf data-binding datagrid observablecollection