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 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')
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.
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>
And here is my C# code:
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.