【问题标题】:How to change foreground of SelectedItem in ListBox?如何更改 ListBox 中 SelectedItem 的前景?
【发布时间】:2014-03-03 08:26:08
【问题描述】:

我昨天问了这个问题并得到了很好的快速回答。但是现在我有一些不同的地方,最后一个解决方案不起作用。

首先让我们看看我的 xaml:

<ListBox ItemsSource="{Binding Children}" x:Name="lst">

    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <WrapPanel IsItemsHost="True" Orientation="Vertical" MaxHeight="{Binding ElementName=lst, Path=ActualHeight}"/>
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>

    <ListBox.Resources>
        <Style TargetType="{x:Type ListBoxItem}">
            <Setter Property="Width" Value="250" />
            <Setter Property="Height" Value="125" />
            <Setter Property="Margin" Value="2.5" />
            <Setter Property="Padding" Value="2.5" />
            <Setter Property="Background" Value="{Binding Background, Converter={StaticResource stringToBrushConverter}}" />
            <Setter Property="Foreground" Value="White" />
            <Setter Property="VerticalContentAlignment" Value="Bottom" />
        </Style>
    </ListBox.Resources>

    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal" Height="125" Width="250">
                <Path Data="{Binding Image}"  VerticalAlignment="Center" 
                      Stretch="Uniform" Fill="#FFFFFFFF"
                      Width="68" Height="68" Margin="10" RenderTransformOrigin="0.5,0.5">
                    <Path.RenderTransform>
                        <TransformGroup>
                            <TransformGroup.Children>
                                <RotateTransform Angle="0" />
                                <ScaleTransform ScaleX="1" ScaleY="1" />
                            </TransformGroup.Children>
                        </TransformGroup>
                    </Path.RenderTransform>
                </Path>
                <TextBlock Text="{Binding Title, Converter={StaticResource spaceToNewLineConverter}}" VerticalAlignment="Top" 
                           Margin="40,10,10,10" FontSize="24" />
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>

</ListBox>

上面的 xaml 工作得很好,输出也很好。但是现在我想改变 SelectedItem 的前景色。

Here你可以找到我昨天问的问题。

现在,昨天提出的问题的解决方案是这样的:

<Style x:Key="BlankListBoxContainerStyle" TargetType="{x:Type ListBoxItem}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ListBoxItem}">
                <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
                                  SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                                  VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
            </ControlTemplate>
        </Setter.Value>
    </Setter>

   <Setter Property="FocusVisualStyle" Value="{x:Null} "/>
</Style>

如果我按照上面的代码更改模板,我会丢失我在列表框上应用的 DataTemplate。

我也尝试过SolidColorBrush 和使用Setters and Triggers。但我运气不好。

【问题讨论】:

    标签: wpf xaml listbox


    【解决方案1】:

    您可以通过在样式中使用触发器来做到这一点。试试这样的:

    <ListBox.Resources>
        <Style TargetType="{x:Type ListBoxItem}">
            <Setter Property="Width" Value="250" />
            <Setter Property="Height" Value="125" />
            <Setter Property="Margin" Value="2.5" />
            <Setter Property="Padding" Value="2.5" />
            <Setter Property="Background" Value="{Binding Background, Converter={StaticResource stringToBrushConverter}}" />
            <Setter Property="Foreground" Value="White" />
            <Setter Property="VerticalContentAlignment" Value="Bottom" />
            <Style.Triggers>
                <Trigger Property="IsSelected" Value="True">
                    <Setter Property="Foreground" Value="Red" />
                </Trigger>
            </Style.Triggers>
        </Style>
    </ListBox.Resources>
    

    【讨论】:

      【解决方案2】:

      在这种情况下,您需要在ListBoxItem 的DataTemplate 中传递Foreground 值。这可以按如下方式完成:

      <ListBox.ItemTemplate>
          <DataTemplate>
              <StackPanel Orientation="Horizontal" 
                          Height="125" 
                          Width="250">
               ...
                  <TextBlock Foreground="{Binding Path=Foreground,
                                                  RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}}}" ... />
              </StackPanel>
          </DataTemplate>
      </ListBox.ItemTemplate>
      

      【讨论】:

      • 不,我认为您误解了我的问题,或者我可能误解了您。如果我什至不使用 BasedOn,在 ListBox.Resources 部分中声明的 Style 可以与 Style BlankListBoxContainerStyle 一起正常工作。但问题是,如果我将 BlankListBoxContainerStyle 与 ListBox.ItemTemplate 一起使用,那么我不会得到正确的输出。
      • 是的,我误解了这个问题。现在我了解到您正在尝试在需要时设置这两个模板。在您的DataTemplate 中尝试此构造可能会有所帮助:&lt;TextBlock Foreground="{Binding Path=Foreground, RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}}}" /&gt;
      • 是的,这解决了问题。感谢您再次解决我的问题。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多