开发者

WPF: More than 1 items rendered as Selected in a ListBox?

开发者 https://www.devze.com 2023-01-24 18:54 出处:网络
I have a ListBox with custom styles applied. It seems sometimes, when I select a color then another (didn\'t press Ctrl/Shift) it seems like 2 items are selected, sometimes even more

I have a ListBox with custom styles applied. It seems sometimes, when I select a color then another (didn't press Ctrl/Shift) it seems like 2 items are selected, sometimes even more

WPF: More than 1 items rendered as Selected in a ListBox?

Whats with this rendering? My XAML looks like

<ListBox ItemsSource="{Binding ThemeColors}" SelectedValue="{Binding Color}" SelectionChanged="ListBox_SelectionChanged" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <WrapPane开发者_运维百科l 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>

UPDATE: 14 Nov

So I followed @Meleak advice and removed all duplicates, there happen to be some. But now have another problem. Sometimes, when I select a color, the border doesn't show, the IsSelected style is not active? But the preview color updates, showing that the binding worked.

http://screenr.com/18c


I think you're having the exact same problem as Gishu has in this Question. Basically, Color is a struct and not a Class and when you select a Color that is represented more than one time in your ListBox then the Selection will fail since it has no way of telling the difference between the two. Example, equal becomes true.

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

I can think of three solutions to your problem.

  1. Remove duplicates from ThemeColors
  2. Change the Collection to SolidColorBrush (which is a Class and not a Struct) and Bind to Color.
  3. Create your own Color class e.g. MyColor.
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号