我有一個應用自定義樣式的ListBox。看來有時候,當我選擇一種顏色,然後另一個(不按Ctrl/Shift鍵),它看起來像2項被選中,有時甚至更WPF:多於一個項目呈現爲列表框中的選定內容?
與此渲染請告訴我?我的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樣式不活動?但預覽顏色更新,顯示綁定工作。
每次都會發生這些特殊顏色嗎?你可以將你的代碼放在/查看模型嗎? – 2010-11-14 09:55:52
是的這個應用程序背後的代碼實際上是視圖模型... http://pastebin.com/p5McmADR – 2010-11-14 13:31:53