當在ComboBox的SelectedValue屬性上調用該函數時,我發現了一個奇怪的行爲GetBindingExpression()
。GetBindingExpression在第二次調用時返回空值
它在第一次調用時工作正常,但在返回的BindingExpression上調用UpdateTarget()之後,此BindingExpression似乎從屬性中刪除。下次來自屬性的GetBindingExpression()返回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();
如果我刪除UpdateTarget(的通話),那麼接下來GetBindingingExpression()
仍然會返回一個有效的BindingExpression。
這種問題不會發生在CheckBox的IsChecked屬性上。它總是可以正常使用CheckBox的IsChecked屬性。
的XAML是象下面這樣:
<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>
爲什麼UpdateTarget()從組合框的SelectedValue屬性擦除BindingExpression?以及爲什麼它可以正常工作CheckBox的IsChecked屬性?
是對項仍選擇的第二陣子? –
你可以用SelectedItem改爲SelectedValue嗎? –
是的,它仍然被選中,因爲你可以看到兩個呼叫是並排的。第一個成功,第二個得到空。我無法使用SelectedItem,因爲BindingSource僅用作輸入,並由未知數量的ComboBoxes共享來初始化它們的列表。綁定是OneTime,否則組合框將互相干擾。 – user1943928