Reputation: 8384
I have a DataGridColumn and it has a Combobox CellTemplate:
<dxg:GridColumn.CellTemplate>
<DataTemplate>
<ComboBox Name="DataItems"
Width="120"
HorizontalAlignment="Center"
VerticalAlignment="Center"
DisplayMemberPath="Value"
ItemsSource="{Binding RowData.Row.DataDictionary}"
SelectedValue="{Binding RowData.Row.Data}"
SelectedValuePath="Key" />
</DataTemplate>
</dxg:GridColumn.CellTemplate>
I need to detect if the user has changed the initial value of the combobox.
If I use the SelectionChanged event, it fires every time (even at creation).
The view is created dinamically so I can't really use a bool value to check if it is just created or been altered.
What event or binding should I use to detect if the user changed the selection and not the initial loading happened?
Upvotes: 2
Views: 8287
Reputation: 8384
The simple solution for me is using the PropertyChanged event.
It has 2 advantages:
Here is the code:
public DataItem FirstDataItem
{
get
{
return firstDataItem;
}
set
{
firstDataItem= value;
if (FirstDataItem!= null)
FirstDataItem.PropertyChanged += (x, y) =>
{
if (y.PropertyName =="Data" )
DoSomething();
};
}
}
Upvotes: 3
Reputation: 513
To me, I will use OneWayToSource binding. And with your code behind handle, you may know: your source is changed by user or your logical processes.
SelectionChanged will be fired after SelectedValue is changed. It does not care how SelectedValue can be updated. When the control is created, wpf resolved SelectedValue by getting action from source. With OneWayToSource binding, SelectedValue will be not updated by code behind after that time.
In your situation, code should be like this:
public event EventHandler SelectedChangedByUser;
public event EventHandler SelectedChangedByCode;
public object SelectedValue
{
get
{
return _selectedVaue;
}
set
{
if(value != _selectedValue)
{
_selectValue = value;
if(NotifyPropertyChanged != null)
{
NotifyPropertyChanged("SelectedValue");
}
if(SelectedChangedByUser != null)
{
SelectedChangedByUser(this, new EventArgs());
}
}
}
}
public void UpdateSelectedValue(object value)
{
if(value != _selectedValue)
{
_selectValue = value;
if(SelectedChangedByCode != null)
{
SelectedChangedByCode(this, new EventArgs());
}
}
}
Upvotes: 3