2010-09-27 25 views
0

我有一個ListView.ItemTemplate以下的ListView:所選行的背景並不在ListView控件更改

<ListView.ItemTemplate> 
    <DataTemplate> 
     <StackPanelName="stackPanel" Orientation="Horizontal"> 
      <TextBoxName="textBoxOrg" 
       Background="Transparent" BorderThickness="0" TextWrapping="Wrap" Text="{BindingOrgText}" 
       IsReadOnly="True"/> 
      <TextBoxName="textBoxNew" 
       Background="Transparent" BorderThickness="0" TextWrapping="Wrap" Text="{BindingNewText}" 
       AcceptsReturn="True"/> 
     </StackPanel> 
    </DataTemplate> 
</ListView.ItemTemplate> 

而下面ListViewItemStyle

<Style TargetType="ListViewItem"> 
    <Setter Property="SnapsToDevicePixels" Value="True" /> 
     <Style.Triggers> 
      <Trigger Property="IsSelected" Value="True"> 
       <Setter Property="Background" Value="LightGoldenrodYellow" /> 
      </Trigger> 
     </Style.Triggers> 
</Style> 

我想更改默認所選項目的「藍色」背景顏色,但使用上述代碼時,選擇項目時不會更改爲「LightGoldenrodYellow」。

我該如何修復代碼才能正常工作?

回答

0

您需要自定義ListViewItem的ControlTemplate。否則它覆蓋(不使用)你定義的Background觸發器。這裏是默認模板自定義(我使用的是被稱爲stylesnooper一個有用的小工具來獲得模板http://wpfwonderland.wordpress.com/2007/01/02/wpf-tools-stylesnooper/):

  <ControlTemplate TargetType="{x:Type ListBoxItem}"> 
       <Border BorderThickness="{TemplateBinding Border.BorderThickness}" Padding="{TemplateBinding Control.Padding}" BorderBrush="{TemplateBinding Border.BorderBrush}" Background="{TemplateBinding Panel.Background}" Name="Bd" SnapsToDevicePixels="True"> 
        <ContentPresenter Content="{TemplateBinding ContentControl.Content}" ContentTemplate="{TemplateBinding ContentControl.ContentTemplate}" ContentStringFormat="{TemplateBinding ContentControl.ContentStringFormat}" HorizontalAlignment="{TemplateBinding Control.HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding Control.VerticalContentAlignment}" SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" /> 
        </Border> 
       <ControlTemplate.Triggers> 
        <Trigger Property="Selector.IsSelected"> 
         <Setter Property="Panel.Background" TargetName="Bd"> 
          <Setter.Value> 
           <DynamicResource ResourceKey="{x:Static SystemColors.HighlightBrushKey}" /> 
           </Setter.Value> 
          </Setter> 
         <Setter Property="TextElement.Foreground"> 
          <Setter.Value> 
           <DynamicResource ResourceKey="{x:Static SystemColors.HighlightTextBrushKey}" /> 
           </Setter.Value> 
          </Setter> 
         <Trigger.Value> 
          <s:Boolean> 
           True</s:Boolean> 
          </Trigger.Value> 
         </Trigger> 
        <MultiTrigger> 
         <MultiTrigger.Conditions> 
          <Condition Property="Selector.IsSelected"> 
           <Condition.Value> 
            <s:Boolean> 
             True</s:Boolean> 
            </Condition.Value> 
           </Condition> 
          <Condition Property="Selector.IsSelectionActive"> 
           <Condition.Value> 
            <s:Boolean> 
             False</s:Boolean> 
            </Condition.Value> 
           </Condition> 
          </MultiTrigger.Conditions> 
         <Setter Property="Panel.Background" TargetName="Bd"> 
          <Setter.Value> 
           <DynamicResource ResourceKey="{x:Static SystemColors.ControlBrushKey}" /> 
           </Setter.Value> 
          </Setter> 
         <Setter Property="TextElement.Foreground"> 
          <Setter.Value> 
           <DynamicResource ResourceKey="{x:Static SystemColors.ControlTextBrushKey}" /> 
           </Setter.Value> 
          </Setter> 
         </MultiTrigger> 
        <Trigger Property="UIElement.IsEnabled"> 
         <Setter Property="TextElement.Foreground"> 
          <Setter.Value> 
           <DynamicResource ResourceKey="{x:Static SystemColors.GrayTextBrushKey}" /> 
           </Setter.Value> 
          </Setter> 
         <Trigger.Value> 
          <s:Boolean> 
           False</s:Boolean> 
          </Trigger.Value> 
         </Trigger> 
        </ControlTemplate.Triggers> 
       </ControlTemplate> 
0

我終於制定出這樣的:

<Style x:Key="myListboxStyle"> 
    <Style.Resources> 
     <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="#CCFFFFFF" /> 
    </Style.Resources> 
</Style> 

和它的作品完美。 感謝你們所有人。

相關問題