2013-04-15 22 views
2

我已經寫在在Win7 WPF風格的列表框中所以我的風格是Windows 8的列表框中選擇顏色

<ListBox> 
<ListBox.Resources> 
    <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="#3399FF" /> 
    <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="#000000" /> 
    <SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}" Color="#3399FF" /> 
    <SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightTextBrushKey}" Color="#000000" /> 
</ListBox.Resources> 
</ListBox> 

此代碼在集中和散焦狀態下的列表框項目相同的選擇。當我在Windows 8下運行我的程序時,此外觀不起作用。 我的代碼中的錯誤在哪裏?

+0

'InactiveSelectionHighlightBrushKey'和'InactiveSelectionHighlightTextBrushKey'不可用在Windows 7/.NET 4。 – Sheridan

回答

3

在Windows 8 ListBoxItem似乎有

<MultiTrigger> 
    <MultiTrigger.Conditions> 
    <Condition Property="IsSelected" Value="true"/> 
    <Condition Property="Selector.IsSelectionActive" Value="false"/> 
    </MultiTrigger.Conditions> 
    <Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/> 
    <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/> 
</MultiTrigger> 

爲它的默認風格,然後使用SystemColors.ControlBrushKeySystemColors.ControlTextBrushKey,所以你可能想太多覆蓋它們在你的資源不活動選擇觸發,

也許類似於:

<ListBox> 
<ListBox.Resources> 
    <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="#3399FF" /> 
    <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="#000000" /> 
    <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="#3399FF" /> 
    <SolidColorBrush x:Key="{x:Static SystemColors.ControlTextBrushKey}" Color="#000000" /> 
    <SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}" Color="#3399FF" /> 
    <SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightTextBrushKey}" Color="#000000" /> 
</ListBox.Resources> 
</ListBox> 

或只是創建一個Style yoursel f(基於默認模板),並直接在這個新的Style上設置顏色,然後將保證適用於任何版本的操作系統,並且不必保留回溯並檢查默認值是否有變化。

0

嘗試使用的ItemTemplate爲:

<ListBox HorizontalContentAlignment="Stretch"> 
       <ListBox.ItemTemplate> 
        <DataTemplate> 
         <Label Margin="-5, -2,-5,-2" Content="{Binding Item}"> 
          <Label.Style> 
           <Style TargetType="Label"> 
            <Style.Triggers> 
             <MultiDataTrigger> 
              <MultiDataTrigger.Conditions> 
               <Condition Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type ListBox}},Path=IsFocused}" Value="False"/> 
               <Condition Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type ListBoxItem}},Path=IsSelected}" Value="True"/> 
              </MultiDataTrigger.Conditions> 
              <Setter Property="Background" Value="CornflowerBlue"/> 
             </MultiDataTrigger> 
             <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type ListBoxItem}},Path=IsSelected}" Value="True"> 
              <Setter Property="Foreground" Value="White"/> 
             </DataTrigger> 
             <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type ListBoxItem}},Path=IsSelected}" Value="False"> 
              <Setter Property="Foreground" Value="Black"/> 
             </DataTrigger> 
            </Style.Triggers> 
           </Style> 
          </Label.Style> 
         </Label> 
        </DataTemplate> 
       </ListBox.ItemTemplate> 
      </ListBox>