簡體   English   中英

WPF列表框。 樣式無法正常工作

[英]WPF Listbox. Style does not work correcty

我有WPF ListBox的問題(請參閱下面的代碼)。 我試圖在鼠標懸停或選中該項目時更改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>

我的列表框如下圖所示。

在此處輸入圖片說明

所選項目的邊框沒有使用樣式應用的顏色。

謝謝,

好吧,不幸的是,這並非易事...您需要更改ListBoxItem默認ControlTemplate 基於這個答案,我試圖使其看起來像我想的那樣。 最重要的元素在<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