2

OK、これに関するオンライン リソースがないとは信じられません。簡単なことをして、ListBoxアイテムが選択され、その親ListBoxがフォーカスを失ったときにアイテムのスタイルを変更したい。

VisualStateManagerこの目的で使用してきましたが、 SelectedSelectedFocusedFocused状態に重複があるため、たとえば ctrl を押しながらアイテムを選択すると、いくつかのバグが導入されました (間違ったアイテムが選択されているように見えます)。s を使用して修正することにしましたが、フォーカスを失っTriggerたときにトリガーする方法がないように見えることがわかりました。ListBox

私の質問は、この動作を実装する正しい方法は何ですか。「SystemColors をオーバーライドする」とは言わないでください...

編集:

ListBoxOK、両方の回答に賛成票を投じましたが、マウスオーバーや既に使用している他のスタイルに問題はありませんが、彼の回答はオリジナルとまったく同じように機能するため、Viv の回答を選択しました。私はすでにSelector添付プロパティの使用法を見てきましたが、試したことはありませんでしIsSelectionActiveた。それは魅力のように機能しました。このタイプの問題のトリガーを使用することをお勧めしますが、これVisualStateManagerは WPF では新しいものです。重複する状態には、回避できるいくつかの問題があることは明らかだと思います。

私の問題の解決策を実装する2つの方法の素晴らしい例を提供してくれたVivとRichardに再び感謝します.

4

2 に答える 2

6

OK、これに関するオンライン リソースがないとは信じられません。ListBox アイテムが選択され、親の ListBox がフォーカスを失ったときに、簡単なことをしてそのスタイルを変更したいと考えています。

とを探しているMultiTriggerと思いますか?IsSelected=trueSelector.IsSelectionActive=false

次のようなもの:

<Grid>
  <Grid.ColumnDefinitions>
    <ColumnDefinition Width="*" />
    <ColumnDefinition Width="*" />
  </Grid.ColumnDefinitions>
  <ListBox Margin="15">
    <ListBox.ItemContainerStyle>
      <Style TargetType="{x:Type ListBoxItem}">
        <Setter Property="Foreground"
                Value="Blue" />
        <Style.Triggers>
          <MultiTrigger>
            <MultiTrigger.Conditions>
              <Condition Property="IsSelected"
                          Value="true" />
              <Condition Property="Selector.IsSelectionActive"
                          Value="false" />
            </MultiTrigger.Conditions>
            <Setter Property="Foreground"
                    Value="Red" />
          </MultiTrigger>
        </Style.Triggers>
      </Style>
    </ListBox.ItemContainerStyle>
    <ListBoxItem Content="A" />
    <ListBoxItem Content="B" />
    <ListBoxItem Content="C" />
  </ListBox>
  <ListBox Grid.Column="1"
            Margin="15">
    <ListBoxItem Content="A" />
    <ListBoxItem Content="B" />
    <ListBoxItem Content="C" />
  </ListBox>
</Grid>

左側のアイテムListBoxが選択され、実際のアイテムがListBoxフォーカスを失った場合、次のForegroundような赤になります。

ここに画像の説明を入力

Foregroundは単なる例です。 を使用して、MultiTrigger必要に応じて を微調整できStyleます。

于 2013-07-10T14:11:51.653 に答える
3

を使用したStyleのはこちらです。私が期待する方法でアイテムを強調表示します。ListBoxItemVisualStateManager

  <Style TargetType="ListBoxItem">
        <Setter Property="Foreground" Value="{DynamicResource ForegroundBrush}"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ListBoxItem">
                    <Border x:Name="Border"
                            Background="Transparent"
                            CornerRadius="3"
                            BorderThickness="1"
                            Padding="2">
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="SelectionStates">
                                <VisualState x:Name="Unselected"/>
                                <VisualState x:Name="Selected">
                                    <Storyboard>
                                        <ColorAnimationUsingKeyFrames Storyboard.TargetName="Border"
                                                        Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)">
                                            <EasingColorKeyFrame KeyTime="0"
                                                                 Value="DodgerBlue"/>
                                        </ColorAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="SelectedUnfocused">
                                    <Storyboard>
                                        <ColorAnimationUsingKeyFrames Storyboard.TargetName="Border"
                                                        Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)">
                                            <EasingColorKeyFrame KeyTime="0"
                                                                 Value="CornflowerBlue"/>
                                        </ColorAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                        <ContentPresenter/>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
于 2013-07-10T13:34:40.920 に答える