views:

343

answers:

2

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.

+1  A: 

It's hard to understand why you are doing this, but I think I can help you understand why is giving you that error.

<toolkit:DataGridTextColumn Header="Name" Binding="{Binding Name}"/> <toolkit:DataGridComboBoxColumn Header="Department" x:Name="_Departmens" SelectedItemBinding="{Binding Department}"/>

You are binding to a property named Name but if you follow the code your items sources (in MyDataGrid) points to MyData. MyData does not appears anywhere inside the MyDataGrid UserControl.

What you have is a property named TestData but won't work either because TestData does not have a property named Name

In my opinion you should forget about data tables and all that stuff and simple create your objects, put them inside a List, uses that list as your data sources and that's it.

HTH

Markust
Why do you think I shouldn't use data tables? Soon here I'll be using a DataSet and I don't want to end up adding more objects only to store data that is inherently available.
mbadawi23
The whole WPF Binding works much better with custom objects. Instead of having a DataSet you can have a List<Employee> (I used an Employee, not sure if this is correct) where Employee has Name and Department. Then, you can Expose List<Employee> as a property somewhere and it's much easier to bind it.
Markust
A: 

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) :).

Goblin
I did some poking. I think I get this error for each empty column. Do you think this is normal behavior then?
mbadawi23
Yes, it is normal behaviour.
Goblin