【问题标题】:WPF: More than 1 items rendered as Selected in a ListBox?WPF:在 ListBox 中呈现为 Selected 的多个项目?
【发布时间】:2011-05-09 17:12:11
【问题描述】:

我有一个应用了自定义样式的 ListBox。有时,当我选择一种颜色然后选择另一种颜色(没有按 Ctrl/Shift)时,似乎选择了 2 个项目,有时甚至更多

这个渲染是怎么回事?我的 XAML 看起来像

<ListBox ItemsSource="{Binding ThemeColors}" SelectedValue="{Binding Color}" SelectionChanged="ListBox_SelectionChanged" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <WrapPanel Margin="3" />
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>
    <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
            <Setter Property="Padding" Value="0" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ListBoxItem}">
                        <Border x:Name="Bd" SnapsToDevicePixels="true" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="1" Padding="1">
                            <Rectangle Width="20" Height="20">
                                <Rectangle.Fill>
                                    <SolidColorBrush Color="{Binding}" />
                                </Rectangle.Fill>
                            </Rectangle>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsSelected" Value="true">
                                <Setter Property="BorderBrush" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
                            </Trigger>
                            <MultiTrigger>
                                <MultiTrigger.Conditions>
                                    <Condition Property="IsSelected" Value="true"/>
                                    <Condition Property="Selector.IsSelectionActive" Value="false"/>
                                </MultiTrigger.Conditions>
                                <Setter Property="BorderBrush" TargetName="Bd" Value="#FF999999"/>
                            </MultiTrigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </ListBox.ItemContainerStyle>
</ListBox>

更新:11 月 14 日

所以我听从了@Meleak 的建议并删除了所有重复项,碰巧有一些。但是现在又有一个问题。有时,当我选择一种颜色时,边框不显示,IsSelected 样式未激活?但是预览颜色会更新,表明绑定有效。

http://screenr.com/18c

【问题讨论】:

  • 这些特定颜色是否每次都会发生?你可以在后面发布你的代码/查看模型吗?
  • 是的,这个应用程序背后的代码实际上是视图模型...pastebin.com/p5McmADR

标签: wpf templates listbox styles


【解决方案1】:

我认为您遇到的问题与 Gishu 在this Question 中遇到的问题完全相同。基本上,Color 是一个结构而不是 Class,当您选择在 ListBox 中多次表示的 Color 时,Selection 将失败,因为它无法分辨两者之间的区别。例如,equal 变为 true。

Color color1 = Colors.AliceBlue; //#FFF0F8FF
Color color2 = Color.FromArgb(255, 240, 248, 255); //#FFF0F8FF
bool equal = (color1 == color2);

我可以为您的问题想出三个解决方案。

  1. 从 ThemeColors 中删除重复项
  2. 将集合更改为 SolidColorBrush(它是一个类而不是结构)并绑定到颜色。
  3. 创建您自己的颜色类,例如我的颜色。

【讨论】:

  • 这也是我遇到同样问题的经历。我会为 #3 投票,不要覆盖 equals,这样列表框将使用引用相等,这就是你想要的
  • 这行得通,但现在,我注意到有时当我选择一种颜色时,IsSelected 样式不会出现。见“更新:11 月 14 日”
  • 我无法重现您的问题。我使用了您发布的 ListBox,当我删除所有重复项时,一切正常。我建议您尝试其他两个选项之一,因为它们没有结构问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-28
  • 1970-01-01
  • 1970-01-01
  • 2019-03-17
  • 2013-03-12
  • 1970-01-01
相关资源
最近更新 更多