Reputation: 33
I started to work with WPF's datagrid and faced with the problem. I have such list
private List<ChainCode> _chainCode = new List<ChainCode>();
where ChainCode is:
public class ChainCode
{
private uint _number;
private byte _code;
public uint Number { get { return _number; } set { _number = value; } }
public byte Code { get { return _code; } set { _number = value; } }
}
So, I want to bind id to such DataGrid:
<DataGrid x:Name="dataGridChainCode" ItemsSource="{Binding _chainCode}" CanUserAddRows="True" IsEnabled="False" AutoGenerateColumns="False" Margin="10,35,18,0" VerticalAlignment="Top">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Number}" IsReadOnly="True" Header="#" Width="60"/>
<DataGridTextColumn Binding="{Binding Code}" Header="code" Width="60"/>
</DataGrid.Columns>
</DataGrid>
but when I launch my program, I can't add any row to datagrid, cuz there is no row, only headers. And I just have no idea how to fix it.
Update: I made such changes:
private ObservableCollection<ChainCode> _chainCode = new ObservableCollection<ChainCode>();
public ObservableCollection<ChainCode> OCChainCode { get { return _chainCode; } set { _chainCode = value; } }
and next in xaml:
ItemsSource="{Binding OCChainCode}"
but there is no response. What I doing wrong?
The one thing i understood is than it's too early for me to use WPF. So, I'll try to fix it later
Upvotes: 0
Views: 983
Reputation: 2095
In order to use data binding, you need to set the DataContext of the part where you want to use the binding. There are several different ways to set the DataContext, a simple way to do it is in the constructor of the UserControl-class. Usually the DataContext is set to a ViewModel (Using the Model-View-ViewModel-pattern), but it can be any class.
public class ViewModel {
private ObservableCollection<ChainCode> _chainCode = new ObservableCollection<ChainCode>();
public ObservableCollection<ChainCode> OCChainCode
{
// No need for a public setter
get { return _chainCode; }
}
}
UserControl-class (the class in [name].xaml.cs file):
public class MyUserControl : UserControl
{
public MyUserControl()
{
DataContext = new ViewModel();
}
}
You can now use the xaml in your question.
<DataGrid x:Name="dataGridChainCode" ItemsSource="{Binding _chainCode}" CanUserAddRows="True" IsEnabled="False" AutoGenerateColumns="False" Margin="10,35,18,0" VerticalAlignment="Top">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Number}" IsReadOnly="True" Header="#" Width="60"/>
<DataGridTextColumn Binding="{Binding Code}" Header="code" Width="60"/>
</DataGrid.Columns>
</DataGrid>
A thing to note, there are no automatic functionality to add new rows in a DataGrid, you have to write this code yourself. I would suggest a button that creates a new ChainCode-object and adds it to the Observable collection, then it will show up in the UI.
Upvotes: 0
Reputation: 1572
Use ObservableCollection instead of List and implement INotifyPropertyChanged, if you want the list to update when you change values of Number or Code.
And make your list property public or protected.
Upvotes: 1
Reputation: 33381
You can't bind to non-public member. Create property
public List<ChainCode> ChainCodeList
{
get { return _chainCode;}
set { _chainCode = value;}
}
and bind to it.
If your list will change at app lifetime you can use ObservableCollection<T> instead.
Upvotes: 1