2017-10-18 6 views
0

WPFToolkitのAutoCompleteBoxを使用しています。選択した項目の背景色を変更する必要がありますが、できません。私は、フォントのスタイル、フォントのサイズ、ちょうど背景を変更することができます。AutoCompleteBoxを設定するListBox SelectedItem背景色

私はSOから多くのソリューションを試しましたが、いずれも機能しませんでした。私がこれまで試した:

Change background color for selected ListBox item

Changing WPF Listbox SelectedItem text color and highlight/background Color using C#

のSelectedItemの背景を変更するには、トリガを使用して動的に

<Style x:Key="myLBStyle" TargetType="ListBoxItem"> 
     <Setter Property="Background" Value="IndianRed" /> 
     <Setter Property="Foreground" Value="WhiteSmoke" /> 
     <Setter Property="Margin" Value="0" /> 
     <Setter Property="FontSize" Value="22" /> 
     <Style.Triggers> 
      <Trigger Property="IsSelected" Value="true"> 
       <Setter Property="Background" Value="Chartreuse" /> 
       <Setter Property="FontStyle" Value="Italic" /> 
       <Setter Property="Foreground" Value="Chartreuse" /> 
      </Trigger> 
     </Style.Triggers> 
    </Style> 

<local:FocusableAutoCompleteBox x:Name="ACBox" Margin="10,32,10,0" 
    Grid.Row="2" FontSize="27" Grid.ColumnSpan="4" Foreground="#FF333333" 
    Background="#FF1700FF" BorderThickness="2" TextChanged="_ACBox_TextChanged" 
    KeyDown="ACBox_KeyDown" Focusable="True" MinimumPopulateDelay="100" 
    MinimumPrefixLength="1" ItemContainerStyle="{DynamicResource ResourceKey=myLBStyle}"> 

を、私はまた、システムの色上書きしようとしました:

 <Style x:Key="myLBStyle" TargetType="ListBoxItem"> 
     <Style.Resources> 
      <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Red" /> 
      <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Green" /> 
     </Style.Resources> 
     <Setter Property="Background" Value="IndianRed" /> 
     <Setter Property="Foreground" Value="WhiteSmoke" /> 
     <Setter Property="Margin" Value="0" /> 
     <Setter Property="FontSize" Value="22" /> 
    </Style> 

他のプロパティをトリガで無条件に設定します。 selecteditemのフォントをイタリック体、太字体、大きくて小さくすることができますが、選択した項目の背景色を変更することはできません。

答えて

0
<Style x:Key="myLBStyle" TargetType="ListBoxItem"> 
    <Setter Property="Background" Value="IndianRed" /> 
    <Setter Property="Foreground" Value="WhiteSmoke" /> 
    <Setter Property="Margin" Value="0" /> 
    <Setter Property="FontSize" Value="22" /> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type ListBoxItem}"> 
       <Grid Background="{TemplateBinding Background}"> 
        <ContentPresenter></ContentPresenter> 
       </Grid> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
    <Style.Triggers> 
     <Trigger Property="IsSelected" Value="true"> 
      <Setter Property="Background" Value="Chartreuse" /> 
      <Setter Property="FontStyle" Value="Italic" /> 
      <Setter Property="Foreground" Value="Chartreuse" /> 
     </Trigger> 
    </Style.Triggers> 
</Style> 
+0

ありがとうございました。出来た。しかし、私はそれがうまくいったのか知りたいのですか? ListBoxをグリッドに追加し、グリッドの背景を設定することを理解しています。しかし、なぜ私はListBoxの選択された色自体を変更できませんでしたか? –

+0

各アイテムの色は、親コントロールの色を継承しない 'ListBoxItem'で定義されています。 – Iron

+0

を参照してください。つまり、コントロールには、ListBox全体のListBoxItemスタイルと、個々のアイテムを保持する別のListBoxItem(子)があります。 –