【发布时间】: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 样式未激活?但是预览颜色会更新,表明绑定有效。
【问题讨论】:
-
这些特定颜色是否每次都会发生?你可以在后面发布你的代码/查看模型吗?
-
是的,这个应用程序背后的代码实际上是视图模型...pastebin.com/p5McmADR
标签: wpf templates listbox styles