gpt4 book ai didi

wpf - 获取 ItemsControl 中的选定项目

转载 作者:行者123 更新时间:2023-12-01 19:35:40 25 4
gpt4 key购买 nike

我有以下代码,它以行和列的形式填充我的用户控件。正在填充的用户控件包含按钮、链接、文本框等。当在特定行/列中的特定用户控件上按下某个按钮时,我需要知道按下该按钮的是哪个用户控件。下面是填充行和列中的用户控件的 XAML

  <ItemsControl ItemsSource="{Binding Templates}" Width="{Binding GridWidth}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Columns="{Binding NumColumns}" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemContainerStyle>
<Style>
<Setter Property="Grid.Column" Value="{Binding ColumnIndex}" />
<Setter Property="Grid.Row" Value="{Binding RowIndex}" />
</Style>
</ItemsControl.ItemContainerStyle>
<ItemsControl.ItemTemplate>
</ItemsControl>

模板基本上是填充在行/列中的用户控件的集合。最好我想在 ViewModel 中执行此操作,但现在代码后面的解决方案也可以工作。

最佳答案

ItemsControl 无法选择项目,只能显示集合。只有一个Selector或者它的后代之一可以选择项目。

对于你的场景,我认为 ListViewGridView会合适的。当用户单击行中的控件时,事件将冒泡到 ListView 并且该项目将被选中。您可以覆盖默认样式,这样它就不会显示为选定的行:WPF ListView turn off selection .

关于wpf - 获取 ItemsControl 中的选定项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18456836/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com