0

I have an appication-level style for ComboBoxItem:

<Style TargetType="{x:Type ComboBoxItem}" x:Key="DefaultComboBoxItemStyle">
    <!-- ... -->
    <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
    <!-- ... -->
</Style>

<Style TargetType="{x:Type ComboBoxItem}" BasedOn="{StaticResource DefaultComboBoxItemStyle}" />

This style is suitable for me in 99% cases. But, there's 1%, when bound objects haven't IsSelected property. I want to override this binding (in particular, clear it at all).

I thought, that this will be possible this way:

        <!-- somewhere in application code -->
        <ComboBox Margin="5" ItemsSource="{Binding Items}" SelectedItem="{Binding SelectedItem}">
            <ComboBox.ItemContainerStyle>
                <Style TargetType="ComboBoxItem" BasedOn="{StaticResource DefaultComboBoxItemStyle}">
                    <Setter Property="IsSelected" Value="False"/>
                </Style>
            </ComboBox.ItemContainerStyle>
        </ComboBox>

But it doesn't work, binding errors still present. Is there any way to achieve what I want in XAML?

4

2 に答える 2

1

ItemContainerStyleデフォルト以外の ComboBox にを設定する代わりに、ローカルに別のデフォルト スタイルを作成することができResourcesます。

<Window.Resources>
    <Style TargetType="ComboBoxItem">
        <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}"/>
        ...
    </Style>
</Window.Resources>
...
<ComboBox ...>
    <ComboBox.Resources>
        <!-- local default style based on "global" default style -->
        <Style TargetType="ComboBoxItem"
               BasedOn="{StaticResource ResourceKey={x:Type ComboBoxItem}}">
            <Setter Property="IsSelected" Value="False"/>
        </Style>
    </ComboBox.Resources>
</ComboBox>
于 2013-06-24T14:42:09.640 に答える
0

アプリケーションのレベル スタイルでフォールバック値を false に設定できます。

<Style TargetType="{x:Type ComboBoxItem}" x:Key="DefaultComboBoxItemStyle">
    <!-- ... -->
    <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay, FallbackValue=False}" />
    <!-- ... -->
</Style>

編集:バインド エラーを回避するために 、FallBack 値をPriority Bindingと組み合わせて試してください。

変更されたアプリケーション スタイルは次のようになります

<Style TargetType="{x:Type ComboBoxItem}" x:Key="DefaultComboBoxItemStyle">
    <!-- ... -->
    <Setter Property="IsSelected">
        <Setter.Value>
            <PriorityBinding FallbackValue="False">
                <Binding Path="IsSelected" Mode="TwoWay" />
            </PriorityBinding>
        </Setter.Value>
    </Setter>
</Style>
于 2013-06-24T15:09:45.633 に答える