我似乎无法清除数据绑定的WPF ListBox的SelectedItems集合.我试过调用ListBox.SelectedItems.Clear(),我尝试将SelectedIndex设置为-1,将SelectedItem设置为null并调用ListBox.UnselectAll().在调试时,似乎分配没有采取或某些东西正在重置SelectedItems集合,但我不知道是什么.我在SelectionChanged回调中放置了一个断点,它永远不会被意外命中,但SelectedItems.Count成员总是至少为1(有时大于1,因为此ListBox的选择模式是MultiExtended).
有没有人见过这个,知道我做错了什么?这个问题看起来和这个问题完全相同: WPF - 如何从ListView中清除选择?
除了那篇文章,Sonny Boy正在使用ListView,而我正在使用ListBox.在任何情况下,投票的答案是调用ListView.UnselectAll(),这在我的情况下不起作用.
我觉得我必须做一些非常明显错误的事情,因为清理选择应该非常简单.
注意:为了清楚起见,我不想从ListBox中删除选择,我只是不想选择任何内容.
谢谢!
<ListBox Background="{StaticResource ResourceKey=DarkGray}" Name="lbx_subimageThumbnails" Margin="6,6,6,0" ItemsSource="{Binding ElementName=lbx_thumbnails, Path=SelectedItem.Swatches}" Style="{StaticResource WPTemplate}" SelectionMode="Extended" Height="{Binding ElementName=sld_swatchRows, Path=Value}" VerticalAlignment="Top" SelectionChanged="lbx_subimageThumbnails_SelectionChanged" DataContext="{Binding}">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel HorizontalAlignment="Stretch" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="Visibility" Value="{Binding Path=Vis}" />
</Style>
</ListBox.ItemContainerStyle>
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Width="270" Height="270" Margin="5,5,5,5" Tag="{Binding}" Name="lbx_swatchThumbnail" Background="{StaticResource ResourceKey=LightGray}" PreviewMouseLeftButtonDown="lbx_swatchThumbnail_PreviewMouseLeftButtonDown" PreviewMouseMove="lbx_swatchThumbnail_PreviewMouseMove">
<Grid.LayoutTransform>
<ScaleTransform CenterX="0" CenterY="0" ScaleX="0.50" ScaleY="0.50" />
</Grid.LayoutTransform>
<Grid.ContextMenu>
<ContextMenu>
<MenuItem Header="Sync selected" Click="btn_syncSwatch_Click" />
<MenuItem Header="Re-export selected" Click="btn_exportSelected_Click"/>
<MenuItem Header="Set as …
Run Code Online (Sandbox Code Playgroud) 我在获取也具有特定属性的唯一元素列表(基于名称)时遇到了一些麻烦。我可以得到其中任何一个的列表,但合并列表是什么让我绊倒。我的 xml 看起来像这样:
<global>
<discipline name="tracks">
<group name="Normal"/>
<group name="Gloss" PackGroup="Diffuse"/>
<group name="Mask"/>
<group name="Diffuse"/>
<group name="Tint" PackGroup="Gloss"/>
</discipline>
<discipline name="trains">
<group name="Mask"/>
<group name="Diffuse" PackGroup="Mask"/>
</discipline>
</global>
Run Code Online (Sandbox Code Playgroud)
使用这个 xpath 查询,我可以很容易地得到一个不同的组元素列表:
/configuration/global/discipline/group[not(@name = following::group/@name)]
Run Code Online (Sandbox Code Playgroud)
获取定义了 PackGroup 属性的组元素列表也很简单:
/configuration/global/discipline/group[@PackGroup]
Run Code Online (Sandbox Code Playgroud)
但是,我似乎无法获得定义了 PackGroup 属性的元素的唯一列表。我试过这个:
/configuration/global/discipline/group[not(@name = following::group/@name) and @PackGroup]
/configuration/global/discipline/group[not(@name = following::group/@name) and contains(@PackGroup, '*')]
Run Code Online (Sandbox Code Playgroud)
不返回任何元素和
/configuration/global/discipline/group[not(@name = following::group/@name) and contains(@PackGroup, '')]
Run Code Online (Sandbox Code Playgroud)
它只返回唯一的列表。有谁知道正确的 xpath 查询是什么才能得到我要找的东西?
(我正在使用 C# 和 xpath 2.0 以防万一)
谢谢!