2012-02-09 33 views
1

我有这个列表框样式,但由于某种原因,我无法根据触发器更改前景色。我想在选择列表框中的项目时将前景色更改为黑色。我搜遍了所有的解决方案似乎并不适用于我的情况。这里是我的列表框...使用触发器更改ListBoxItem.Foreground颜色

<ListBox IsSynchronizedWithCurrentItem="True" SelectedValuePath="RecordId" ItemsSource="{Binding People}" SelectedValue="{Binding SelectedPersonId}" HorizontalAlignment="Stretch" Margin="20,5,10,5" Width="Auto" Grid.Row="1" Background="{x:Null}" BorderBrush="{x:Null}" x:Name="lstCustomers"> 
    <ListBox.ItemTemplate> 
     <DataTemplate> 
      <Grid Height="Auto" Width="Auto" d:DesignWidth="545.375" d:DesignHeight="50.294"> 
       <Grid.ColumnDefinitions> 
        <ColumnDefinition Width="*"/> 
        <ColumnDefinition Width="132.375"/> 
       </Grid.ColumnDefinitions> 
       <StackPanel x:Name="stackPanel" Orientation="Horizontal" HorizontalAlignment="Stretch"> 
        <Label FontSize="16" Content="{Binding FullName}" Foreground="White"/> 
        <Label FontSize="16" Content="{Binding DisplayName}" Margin="5,0,0,0" Foreground="White"/> 
       </StackPanel> 
       <DockPanel LastChildFill="True" HorizontalAlignment="Stretch" Margin="0" Grid.Column="1"> 
        <Label Content="{Binding Date}" ContentStringFormat="yyyy/MM/dd" FontSize="24" HorizontalAlignment="Right" Foreground="White" DockPanel.Dock="Right" VerticalAlignment="Center"/> 
       </DockPanel> 
      </Grid> 
     </DataTemplate> 
    </ListBox.ItemTemplate> 
    <ListBox.ItemContainerStyle> 
     <Style TargetType="{x:Type ListBoxItem}"> 
      <Setter Property="FocusVisualStyle" Value="{x:Null}" /> 
      <Setter Property="Template"> 
       <Setter.Value> 
        <ControlTemplate TargetType="ListBoxItem"> 
         <Border BorderBrush="#26FFFFFF" BorderThickness="3" CornerRadius="5" Name="Border" Margin="0,0,2,3" Padding="0" SnapsToDevicePixels="true"> 
          <ContentPresenter /> 
         </Border> 
         <ControlTemplate.Triggers> 
          <Trigger Property="IsSelected" Value="true"> 
           <Setter TargetName="Border" Property="Background" Value="#7FFFFFFF" /> 
          </Trigger> 
          <Trigger Property="IsMouseOver" Value="true"> 
           <Setter TargetName="Border" Property="Background" Value="#0FFFFFFF"></Setter> 
           <Setter TargetName="Border" Property="BorderBrush" Value="#BEFFFFFF"></Setter> 
          </Trigger> 
          <MultiTrigger> 
           <MultiTrigger.Conditions> 
            <Condition Property="Selector.IsSelected" Value="true"></Condition> 
            <Condition Property="IsMouseOver" Value="true"></Condition> 
           </MultiTrigger.Conditions> 
           <Setter TargetName="Border" Property="Background" Value="#7FFFFFFF"/> 
          </MultiTrigger> 
         </ControlTemplate.Triggers> 
        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 
     </Style> 
    </ListBox.ItemContainerStyle> 
</ListBox> 

任何帮助,将不胜感激!

谢谢!

+0

? – 2012-02-09 18:26:16

+0

是的,这是正确的。问题是我无法在边框控件上设置前景,所以我不知道如何在模板中设置引用数据模板中实际项目的样式... – Lukasz 2012-02-09 19:49:08

回答

2

你的触发器做工精细,但我没有看到任何指定当选择一个项目的前景应该是黑色的....

<ControlTemplate TargetType="ListBoxItem"> 
     <Border BorderBrush="#26FFFFFF" BorderThickness="3" CornerRadius="5" Name="Border" Margin="0,0,2,3" Padding="0" SnapsToDevicePixels="true"> 
      <ContentPresenter /> 
     </Border> 
     <ControlTemplate.Triggers> 
      <Trigger Property="IsSelected" Value="true"> 
       <Setter TargetName="Border" Property="Background" Value="#7FFFFFFF" /> 
       <Setter TargetName="Border" Property="Foreground" Value="Black" /> 
      </Trigger> 
      <Trigger Property="IsMouseOver" Value="true"> 
       <Setter TargetName="Border" Property="Background" Value="#0FFFFFFF"></Setter> 
       <Setter TargetName="Border" Property="BorderBrush" Value="#BEFFFFFF"></Setter> 
      </Trigger> 
      <MultiTrigger> 
       <MultiTrigger.Conditions> 
        <Condition Property="Selector.IsSelected" Value="true"></Condition> 
        <Condition Property="IsMouseOver" Value="true"></Condition> 
       </MultiTrigger.Conditions> 
       <Setter TargetName="Border" Property="Background" Value="#7FFFFFFF"/> 
       <Setter TargetName="Border" Property="Foreground" Value="Black" /> 
      </MultiTrigger> 
     </ControlTemplate.Triggers> 
    </ControlTemplate> 

我看到代码,设置Foreground颜色但是白色Label。这将覆盖您在ListBoxItem样式中设置的任何前景色。

我建议将默认的前景色为您ListBoxItem风格,无论是Label前景色结合ListBoxItem前景色,或使用TextBlock代替Label将默认继承前景色。

<Style TargetType="{x:Type ListBoxItem}"> 
    <Setter Property="Foreground" Value="White" /> 
    <Setter Property="FocusVisualStyle" Value="{x:Null}" /> 
    <Setter Property="Template"> 
     ... 
    </Setter> 
</Style> 

而且

<Label Text="Name" Foreground="{Binding RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}}, Path=Foreground}"/> 

或者

你想选择列表框时,更改标签,以黑色的前景色
<TextBlock Text="Name" /> 
+1

前两个字符用于Alpha通道。 – Lukasz 2012-02-09 17:48:39

+0

@Lukasz是的,而且WPF不会读这样的alpha通道。摆脱他们,它会正常工作。如果要设置透明度,可以使用“不透明度”属性 – Rachel 2012-02-09 18:23:08

+0

从颜色值中删除Alpha通道会破坏我的风格。如果在触发器IsSelected中删除了7F(127),则所选项目现在完全变白。我试着做但它不起作用。你能给我一个你如何修复它的例子吗?此外,如果WPF不能像那样工作,Blend会如何设置Alpha通道的所有颜色?谢谢! – Lukasz 2012-02-09 19:03:46