如何正确地将对象绑定到WPF DataGrid? [英] How to correctly bind an object to a WPF DataGrid?
问题描述
我正在尝试从正在构建的用户控件中获取WPF DataGrid。事情似乎一切正常。但是我在IDE的输出窗口中注意到了此消息:
I'm trying to get a WPF DataGrid to work from a user control I'm building. Things seems to work fine. But I noticed this message in the Output window in the IDE:
System.Windows.Data错误:39:BindingExpression路径错误: Name属性不正确在'object'``Object'(HashCode = 18165668)'上找到。 BindingExpression:Path =名称; DataItem =对象(HashCode = 18165668);目标元素是 TextBlock(名称=);目标属性为文本(类型为字符串)
System.Windows.Data错误:39:BindingExpression路径错误:在对象,对象(HashCode = 18165668)上找不到部门属性。 BindingExpression:Path =名称; DataItem =对象(HashCode = 18165668);目标元素是 TextBlockComboBox(名称=);目标属性是 SelectedItem(类型为字符串)
System.Windows.Data Error: 39 : BindingExpression path error: 'Name' property not found on 'object' ''Object' (HashCode=18165668)'. BindingExpression:Path=Name; DataItem='Object' (HashCode=18165668); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String') System.Windows.Data Error: 39 : BindingExpression path error: 'Department' property not found on 'object' ''Object' (HashCode=18165668)'. BindingExpression:Path=Name; DataItem='Object' (HashCode=18165668); target element is 'TextBlockComboBox' (Name=''); target property is 'SelectedItem' (type 'String')
我想要做的是从XAML手动向DataGrid添加列并将其绑定
What I'm trying to do is to manually add columns to DataGrid from XAML and bind them to an object that I have in the C# code.
这是我的XAML代码:
Here is my XAML code:
<UserControl x:Class="Sting.Utilities.MyDataGrid" Name="This"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:toolkit="http://schemas.microsoft.com/wpf/2008/toolkit"
Height="600" Width="800">
<Grid>
<toolkit:DataGrid AutoGenerateColumns="False" Name="myDataGrid" Margin="10" ItemsSource="{Binding ElementName=This, Path=MyData}">
<toolkit:DataGrid.Columns>
<toolkit:DataGridTextColumn Header="Name" Binding="{Binding Name}"/>
<toolkit:DataGridComboBoxColumn Header="Department" x:Name="_Departmens" SelectedItemBinding="{Binding Department}"/>
</toolkit:DataGrid.Columns>
</toolkit:DataGrid>
</Grid>
</UserControl>
这是我的C#代码:
namespace Sting.Utilities
{
///
/// Interaction logic for UserControl1.xaml
///
public partial class MyDataGrid : UserControl
{
DataTable _myData;
public DataTable TestData { get { return _testData; } }
public MyDataGrid()
{
// Initialize data table
_myData = new DataTable();
_testData.Columns.Add(new DataColumn("Name", typeof(string)));
_testData.Columns.Add(new DataColumn("Department", typeof(string)));
// Temp Code: User should add rows
DataRow row = _testData.NewRow();
row["Name"] = "John Smith";
row["Department"] = "Accounting";
_testData.Rows.Add(row);
// Initialize combo boxes
List departmentComboBoxList = new List() {"Accounting", "Purchasing", "Engineering"};
_Departments.ItemsSource = departmentComboBoxList;
}
}
}
任何想法都值得赞赏。谢谢。
Any thoughts are appreciated. Thank you.
推荐答案
是否显示新商品占位符行?因为如果是的话,那就是导致输出绑定错误的一个。
Is the 'New item placeholder' row shown? Because if it is, that's the one causing the output binding error.
占位符项目的数据上下文是一个空对象-自然不具有另一个对象的属性
The datacontext for a placeholder item is an empty object - which naturally does not have the properties of the other rows.
所以,一切都很好-如果多次收到同一条消息(每行一个,然后一个占位符),您就会遇到麻烦。
So, all is good - you are in trouble if you get the same message more than once (one per row and then one for the placeholder) :).
这篇关于如何正确地将对象绑定到WPF DataGrid?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!