I have a DataGrid with several fields, one of it is a DataGridComboBoxColumn. On that one I add items dynamically depending on one field by default "Add items". If I have added Item 1
and is currently selected, I'd like that when the user clicks on the dropdown and selects Item 1
again I can trigger my source and edit that item. However currently using UpdateSourceTrigger=PropertyChanged
does not trigger any changes.
Looks like UpdateSourceTrigger=Explicit
is what I need, but I am not sure how to implement the UpdateSource() method to do what I want.
My xaml snippet
<DataGridComboBoxColumn x:Name="ItemColumn" Header="{x:Static res:RES.HEADER}"
ToolTipService.ToolTip="{x:Static res:RES.TOOLTIP}"
SelectedValueBinding="{Binding Path=Item, UpdateSourceTrigger=PropertyChanged, NotifyOnValidationError=True}" >
<DataGridComboBoxColumn.ElementStyle>
<Style TargetType="ComboBox" BasedOn="{StaticResource DataGridComboBoxElementStyle}">
<Setter Property="ItemsSource" Value="{Binding Items, UpdateSourceTrigger=PropertyChanged}" />
</Style>
</DataGridComboBoxColumn.ElementStyle>
<DataGridComboBoxColumn.EditingElementStyle>
<Style TargetType="ComboBox" BasedOn="{StaticResource DataGridComboBoxEditingElementStyle}">
<Setter Property="ItemsSource" Value="{Binding Items, UpdateSourceTrigger=PropertyChanged}" />
</Style>
</DataGridComboBoxColumn.EditingElementStyle>
</DataGridComboBoxColumn>
And I got string
Item and a List<string>
Items properties to bind the fields.
Is it possible to get an example of how to use Explicit to get a trigger every time the user clicks on the dropdown of the Item, even if already selected? or is there any other way not using Explicit? I am using MVVM as much as possible so that's also a factor to consider.
CodePudding user response:
The SelectedValueBinding
source property is only set if you select a new value.
If you want to do something when the the currently selected value is selected again, you could handle this in the view by for example handling the DropDownOpened
and DropDownClosed
events:
private void ComboBox_Loaded(object sender, RoutedEventArgs e)
{
ComboBox cmb = (ComboBox)sender;
cmb.DropDownOpened = Cmb_DropDownOpened;
cmb.DropDownClosed = Cmb_DropDownClosed;
}
string _selectedValue;
private void Cmb_DropDownOpened(object sender, EventArgs e)
{
_selectedValue = ((ComboBox)sender).SelectedItem as string;
}
private void Cmb_DropDownClosed(object sender, EventArgs e)
{
string selectedValue = ((ComboBox)sender).SelectedItem as string;
if (selectedValue == _selectedValue)
{
//same value was selected...
}
}
XAML:
<DataGridComboBoxColumn.EditingElementStyle>
<Style TargetType="ComboBox">
<Setter Property="ItemsSource" Value="{Binding Items, UpdateSourceTrigger=PropertyChanged}" />
<EventSetter Event="Loaded" Handler="ComboBox_Loaded" />
</Style>
</DataGridComboBoxColumn.EditingElementStyle>