11

thisによると、ControlBrushKey リソースをオーバーライドすると、ListBox で選択された項目がフォーカスされていないときにその背景色を変更する必要があります。これを反証する簡単な例を作成しました。

 <StackPanel>
    <ListBox>
      <ListBox.Resources>
        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="LightBlue"/>
        <!--SelectedItem without focus but doesn't really work-->
        <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Orange" />
      </ListBox.Resources>
      <ListBoxItem>
        Item 1
      </ListBoxItem>
      <ListBoxItem>
        Item 2
      </ListBoxItem>
    </ListBox>
    <TextBox></TextBox>
  </StackPanel>

これを .NET 4.5 で実行すると、フォーカスのある色のみが変更され、フォーカスされていない色は変更されないことがわかります (.NET 4.0 で動作します)。理由はありますか?

編集:これはList/Combo Box Background And Selected Colors Under .net 4.5の複製のようです。

4

4 に答える 4

1

コントロールのデフォルトテンプレートがすべてです。.NET 4 のようにシステム カラーを使用しない場合、何も変更されません。

于 2012-10-03T14:10:44.917 に答える
0

これは、システムの色やコントロール テンプレートの変更を伴わない、私が思いついたものです。ListBox を新しい UserControl でラップするだけです。

public partial class StyledListBox : UserControl
{
    public DataTemplate ItemTemplate
    {
        get { return (DataTemplate)GetValue(ItemTemplateProperty); }
        set { SetValue(ItemTemplateProperty, value); }
    }

    public IEnumerable ItemsSource
    {
        get { return (IEnumerable)GetValue(ItemsSourceProperty); }
        set { SetValue(ItemsSourceProperty, value); }
    }

    public object SelectedItem
    {
        get { return GetValue(SelectedItemProperty); }
        set { SetValue(SelectedItemProperty, value); }
    }

    public StyledListBox()
    {
        InitializeComponent();
    }

    public static readonly DependencyProperty ItemTemplateProperty = DependencyProperty.Register("ItemTemplate", typeof(DataTemplate), typeof(StyledListBox), new FrameworkPropertyMetadata(null));
    public static readonly DependencyProperty ItemsSourceProperty = DependencyProperty.Register("ItemsSource", typeof(IEnumerable), typeof(StyledListBox), new FrameworkPropertyMetadata(null));

    public static readonly DependencyProperty SelectedItemProperty = DependencyProperty.Register("SelectedItem", typeof(object), typeof(StyledListBox), new FrameworkPropertyMetadata(null)
    {
        BindsTwoWayByDefault = true,
        DefaultUpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
    });
}

XAML:

<UserControl x:Class="StyledListBox"

     <ListBox ItemsSource="{Binding ItemsSource, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type common:StyledListBox}}}"
              SelectedItem="{Binding SelectedItem, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type common:StyledListBox}}}">

        <ListBox.ItemTemplate>
            <DataTemplate>
                <Border>
                    <Border.Style>
                        <Style TargetType="{x:Type Border}">
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding IsSelected, UpdateSourceTrigger=PropertyChanged, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBoxItem}}}"
                                             Value="True">
                                    <Setter Property="Background" Value="Red" />
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </Border.Style>

                    <ContentPresenter ContentTemplate="{Binding ItemTemplate, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type StyledListBox}}}" />
                </Border>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</UserControl>

次に、ラッパーの UserControl を ListBox のように使用します。制御したいその他の ListBox プロパティは、私の例ItemsSourceと同じ方法で簡単にラッパーに追加できます。SelectedItem

于 2015-01-30T14:28:52.597 に答える
-2

これに対する解決策は追加です

FrameworkCompatibilityPreferences.AreInactiveSelectionHighlightBrushKeysSupported = false;

電話する前に

InitializeComponent();
于 2013-09-18T22:46:55.667 に答える