简体   繁体   English

WPF列表框。 样式无法正常工作

[英]WPF Listbox. Style does not work correcty

I've a problem with a WPF ListBox ( See code bellow ). 我有WPF ListBox的问题(请参阅下面的代码)。 I'm trying to change the background and foreground for the listBox items when the mouse is over or when the item is selected but it doesn't work and I don't know why. 我试图在鼠标悬停或选中该项目时更改listBox项目的背景和前景,但是它不起作用,我也不知道为什么。

<ListBox x:Name="ListBoxSnapshots" ItemsSource="{Binding DevicesImageList}" SelectedIndex="{Binding WebcamSelected}" BorderThickness="0" SelectionMode="Single" HorizontalContentAlignment="Left" VerticalContentAlignment="Stretch" SelectionChanged="ListBoxSnapshots_SelectionChanged" ScrollViewer.VerticalScrollBarVisibility="Disabled" >
<ListBox.ItemContainerStyle>
    <Style TargetType="{x:Type ListBoxItem}">
        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="Background" Value="#FFBCBCBC"/>
                <Setter Property="Foreground" Value="Black"/>
            </Trigger>
            <Trigger Property="IsMouseOver" Value="False">
                <Setter Property="Background" Value="#FFD3D3D3"/>
                <Setter Property="Foreground" Value="Black"/>
            </Trigger>
            <Trigger Property="IsSelected" Value="True">
                <Setter Property="Background" Value="#FFD3D3D3"/>
                <Setter Property="Foreground" Value="Black"/>
            </Trigger>
            <Trigger Property="IsSelected" Value="false">
                <Setter Property="Background" Value="White"/>
                <Setter Property="Foreground" Value="Black"/>
            </Trigger>
        </Style.Triggers>
    </Style>
</ListBox.ItemContainerStyle>
<ListBox.ItemTemplate>
    <DataTemplate>
        <Border BorderThickness="2" BorderBrush="DarkGray">
            <StackPanel Orientation="Horizontal">
                <Image Source="{Binding Path=theImage}" Stretch="Fill" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Width="142" Height="80"/>
                <Canvas Width="142" Height="80" Margin="-142,0,0,0">
                    <Image Source="/MyApp;component/Images/snapshot-whitetriangle.png" Stretch="Fill" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" />
                    <Label FontSize="8" Content="{Binding Path=theImageIndex}" Foreground="DarkGray" FontWeight="DemiBold" Canvas.Top="61" Canvas.Left="110" HorizontalAlignment="Center" VerticalAlignment="Top" Width="34" Height="24" VerticalContentAlignment="Top" HorizontalContentAlignment="Right" />
                </Canvas>
            </StackPanel>
        </Border>
    </DataTemplate>
</ListBox.ItemTemplate>
<ListBox.ItemsPanel>
    <ItemsPanelTemplate>
        <WrapPanel IsItemsHost="True" Orientation="Horizontal" />
    </ItemsPanelTemplate>
</ListBox.ItemsPanel>

My listBox looks like the following picture. 我的列表框如下图所示。

在此处输入图片说明

The border of the Item selected has not the color that I have applied using Style. 所选项目的边框没有使用样式应用的颜色。

Thanks, 谢谢,

Well, unfortunately it's not so easy... You need to change default ControlTemplate of ListBoxItem . 好吧,不幸的是,这并非易事...您需要更改ListBoxItem默认ControlTemplate Basing on this answer I've tried to make it look as i thought you wanted it to look. 基于这个答案,我试图使其看起来像我想的那样。 The most important elements are in <Setter Property="Template"> setter. 最重要的元素在<Setter Property="Template">设置器中。

<Style TargetType="{x:Type ListBoxItem}">
  <Setter Property="SnapsToDevicePixels" Value="True" />
  <Setter Property="Padding" Value="4,1" />
  <Setter Property="HorizontalContentAlignment" Value="{Binding HorizontalContentAlignment,
                  RelativeSource={RelativeSource FindAncestor,
                                                 AncestorLevel=1,
                                                 AncestorType={x:Type ItemsControl}}}" />
  <Setter Property="VerticalContentAlignment" Value="{Binding VerticalContentAlignment,
                  RelativeSource={RelativeSource FindAncestor,
                                                 AncestorLevel=1,
                                                 AncestorType={x:Type ItemsControl}}}" />
  <Setter Property="Background"
      Value="Transparent" />
  <Setter Property="BorderBrush"
      Value="Transparent" />
  <Setter Property="BorderThickness"
      Value="1" />
  <Setter Property="FocusVisualStyle">
    <Setter.Value>
      <Style>
        <Setter Property="Control.Template">
          <Setter.Value>
            <ControlTemplate>
              <Rectangle Margin="2"
                  SnapsToDevicePixels="True"
                  Stroke="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"
                  StrokeDashArray="1 2"
                  StrokeThickness="1" />
            </ControlTemplate>
          </Setter.Value>
        </Setter>
      </Style>
    </Setter.Value>
  </Setter>
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate TargetType="{x:Type ListBoxItem}">
        <Border x:Name="Bd"
            Background="White"
            BorderBrush="DarkGray"
            BorderThickness="{TemplateBinding BorderThickness}"
            Padding="{TemplateBinding Padding}"
            SnapsToDevicePixels="True">
          <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
              VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
              Content="{TemplateBinding Content}"
              ContentStringFormat="{TemplateBinding ContentStringFormat}"
              ContentTemplate="{TemplateBinding ContentTemplate}"
              SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
        </Border>
        <ControlTemplate.Triggers>
          <MultiTrigger>
            <MultiTrigger.Conditions>
              <Condition Property="IsMouseOver"
                  Value="True" />
            </MultiTrigger.Conditions>
            <Setter TargetName="Bd"
                Property="Background"
             Value="#FFBCBCBC" />
          </MultiTrigger>
          <MultiTrigger>
            <MultiTrigger.Conditions>
              <Condition Property="Selector.IsSelectionActive"
                  Value="False" />
              <Condition Property="IsSelected"
                  Value="True" />
            </MultiTrigger.Conditions>
            <Setter TargetName="Bd"
                Property="Background"
                Value="#FFD3D3D3" />
          </MultiTrigger>
          <MultiTrigger>
            <MultiTrigger.Conditions>
              <Condition Property="Selector.IsSelectionActive"
                  Value="True" />
              <Condition Property="IsSelected"
                  Value="True" />
            </MultiTrigger.Conditions>
            <Setter TargetName="Bd"
                Property="Background"
                Value="#FFD3D3D3" />
          </MultiTrigger>
          <Trigger Property="IsEnabled"
              Value="False">
            <Setter TargetName="Bd"
                Property="TextElement.Foreground"
                Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}" />
          </Trigger>
        </ControlTemplate.Triggers>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM