Reputation: 171
I found a strange behavior of GetBindingExpression()
, when the function is called on a ComboBox SelectedValue property.
It works fine on the first time it is called, but after UpdateTarget() is called on the returned BindingExpression, this BindingExpression seems deleted from the property. The next time GetBindingExpression() from the property returns null.
BindingExpression exp;
// call it first time, returns an expression, and UpdateTarget success.
exp = ((ComboBox)obj).GetBindingExpression(ComboBox.SelectedValueProperty);
// call UpdateTarget
if (exp != null) exp.UpdateTarget();
// call it the second time, and now it returns null!
exp = ((ComboBox)obj).GetBindingExpression(ComboBox.SelectedValueProperty);
if (exp != null) exp.UpdateTarget();
If I remove the calling of UpdateTarget(), then the next GetBindingingExpression()
still returns a valid BindingExpression.
This kind of problem does not happen on a CheckBox's IsChecked property. It always works fine with IsChecked property of a CheckBox.
The XAML is like below:
<Style TargetType="{x:Type ComboBox}" x:Key="GainComboBoxStyle_0x00000022">
<Setter Property="Height" Value="20" />
<Setter Property="MaxWidth" Value="80" />
<Setter Property="FontFamily" Value="Calibri"/>
<Setter Property="FontSize" Value="15"/>
<Setter Property="FontWeight" Value="UltraBold"/>
<Setter Property="Tag" Value="{StaticResource moduleGain_0x00000022}" />
<Setter Property="Visibility" Value="{Binding Mode=OneWay, Converter={StaticResource getVisibilityOfGainConverter}}"/>
<Style.Triggers>
<DataTrigger Binding="{Binding Mode=OneWay, Converter={StaticResource isShowingGain}}" Value="True">
<Setter Property="SelectedValue" Value="{Binding Path=., RelativeSource={RelativeSource Mode=Self}, Mode=OneWay, Converter={StaticResource getGainValue}}"/>
<Setter Property="DisplayMemberPath" Value="Name" />
<Setter Property="SelectedValuePath" Value="Name" />
<Setter Property="IsSynchronizedWithCurrentItem" Value="False" />
<Setter Property="ItemsSource" Value="{Binding Path=GainQ13Indices, Mode=OneTime}" />
</DataTrigger>
</Style.Triggers>
</Style>
<Style TargetType="{x:Type CheckBox}" x:Key="EnableCheckBoxStyle_0x00000010">
<Setter Property="MaxWidth" Value="16"/>
<Setter Property="Tag" Value="{StaticResource
moduleEnable_0x00000010}" />
<Setter Property="Visibility" Value="{Binding Mode=OneWay, Converter={StaticResource getVisibilityOfEnableConverter}}"/>
<Style.Triggers>
<DataTrigger Binding="{Binding Mode=OneWay, Converter={StaticResource isShowingEnable}}" Value="True">
<Setter Property="IsChecked" Value="{Binding Path=., RelativeSource={RelativeSource
Mode=Self}, Mode=OneWay,
Converter={StaticResource
moduleEnableDisableConverter} }" />
</DataTrigger>
</Style.Triggers>
</Style>
Why UpdateTarget() erases the BindingExpression from the SelectedValue property of a ComboBox? and why it works fine for IsChecked property of a CheckBox?
Upvotes: 3
Views: 1326
Reputation: 6413
Ensure that your SelectedValue is being set to one of the actual object's contained in GainQ13Indices - it looks like you might be generating one in your converter, which can cause the bindings to break. It is expected that SelectedValue is set to something that is contained within the collection that is bound to ItemsSource.
I'm guessing it works for IsChecked because that is a boolean, which is a value type, while it looks like what you're using in your ComboBox is a reference type (where equality checks are a bit more involved).
I've ran into these sorts of problems before, and I've gotten in the habit of exposing an extra property in the view model to represent the selected value for each list that is exposed, and avoided trying to do any tampering with the binding in code.
For example (in the view model):
public ObservableCollection<Gain> GainQ13Indices { get; private set; }
public Gain SelectedGainQ13Indice { get; set; }
Upvotes: 1