Reputation: 9692
Two int columns and both are editable. The default behavior is that each value in column B should at least be equal to each corresponding record value entered in column A.
The idea is to have some sort of OneWay DataBinding from ColumnA to ColumnB. If this was an individual labels scenario, it would have been easy to accomplish in XAML; how about binding columns? Is it possible, and if yes how?
<DataGrid>
<DataGrid.Columns>
<DataGridTextColumn Header="A" />
<DataGridTextColumn Header="B" />
</DataGrid.Columns>
</DataGrid>
Upvotes: 1
Views: 86
Reputation: 132548
Start by creating your Data Object to represent each row of data
public class MyDataObject : INotifyPropertyChanged
{
// properties are simplified - google INotifyPropertyChanged to setup correctly
public int A { get; set; }
public int B { get; set; }
}
Next make a collection of those objects, and set it to the DataGrid.ItemsSource
(a better idea is to bind the property, but I'm keeping it simple here)
var items = new ObservableCollection<MyDataObject>();
// add items to collection here
myDataGrid.ItemsSource = items;
Make sure your DataBindings in your XAML are setup correctly
<DataGrid x:Name="myDataGrid">
<DataGrid.Columns>
<DataGridTextColumn Header="A" Binding="{Binding A}" />
<DataGridTextColumn Header="B" Binding="{Binding B}" />
</DataGrid.Columns>
</DataGrid>
And then last of all implement any specific logic or validations you wish in your MyDataObject
class. If you wish to prevent B from being greater than A, add that logic check in the set
accessor method for B
. Here's one possible example :
public int B
{
get { return _b; }
set
{
if (value < A)
return;
_b = value;
RaisePropertyChanged("B");
}
}
Or for more advanced validations, you could also choose to implement IDataErrorInfo
Upvotes: 4