【问题标题】:ListBoxItem IsSelected styleListBoxItem IsSelected 样式
【发布时间】:2011-02-05 01:01:26
【问题描述】:

我还是没听懂。你能否告诉我如何覆盖 ListBox 的默认行为。 每次选择 ListBoxItem 时,都应更改边框的背景。不是整行的背景,而是指定的边框的背景。

 <ListBox ItemsSource="{Binding Source={StaticResource AssetsViewSource}}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Border BorderThickness="2" BorderBrush="Black">
                    <StackPanel>
                        <TextBlock Text="Name: " />
                        <TextBlock Text="{Binding Name}" />
                    </StackPanel>
                </Border>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

【问题讨论】:

    标签: wpf listbox styles listboxitem


    【解决方案1】:

    只需将以下内容添加到 ListBox 项标记中

    <ListBox.Resources>
    <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent" />
    </ListBox.Resources>
    

    这应该可以解决问题..

    【讨论】:

    • 我现在没有时间检查,仍然认为这是一个正确的答案和解决方案。谢谢。
    【解决方案2】:

    使用 DataTemplate 的 Triggers 集合,通过 RelativeSource 将您带到包含的 ListBoxItem:

    <DataTemplate>
      <Border BorderThickness="2" BorderBrush="Black" Name="Bd">
        <StackPanel>
          <TextBlock Text="Name: " />
          <TextBlock Text="{Binding Name}" />
        </StackPanel>
      </Border>
      <DataTemplate.Triggers>
        <DataTrigger Value="True"
                     Binding="{Binding 
                                  IsSelected, 
                                  RelativeSource={RelativeSource 
                                      AncestorType={x:Type ListBoxItem}}}">
          <!-- everybody loves HotPink -->
          <Setter TargetName="Bd" Property="Background" Value="HotPink"/>  
        </DataTrigger>
      </DataTemplate.Triggers>
    </DataTemplate>
    

    【讨论】:

    • 是的,它工作...但它并没有摆脱默认的蓝色背景.. 怎么做?
    • 哦,我想通了。我只是放了默认 的副本并切断了所有触发器。但我不确定这是最好的方法......
    • 你说的“默认”蓝色背景,是指自动选择整行的颜色背景吗?如果是这样,你做对了:ItemContainerStyle 是摆脱它的唯一方法。很抱歉没有在原始答案中涵盖这一点 - 当您说“不是整行的背景”并认为您想将自动选择颜色留在那里时,我误解了。
    • 每个人都喜欢 HotPink! +1
    猜你喜欢
    • 2010-10-19
    • 2010-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-21
    • 1970-01-01
    相关资源
    最近更新 更多