在ItemsControl中获取选定的项目 [英] Getting selected item in ItemsControl

查看:678
本文介绍了在ItemsControl中获取选定的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have the following code which populates my user control in form of rows and column. The user control which is being populated contains Button, links, textbox etc. When a certain button is pressed on a particular User Control in a particular row/column, I need to know for which User Control that button was pressed. Here is the XAML that is populating the user controls in rows and columns

  <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>

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

Templates is basically a collection of UserControls that are being populated in Rows/Columns. Preferably I want to do this in ViewModel but solution in code behind for now will work as well.

推荐答案

ItemsControl无法选择项目,只能显示收藏集.仅选择器或其中之一后代可以选择物品.

ItemsControl can't select items, only present collections. Only a Selector or one of it's descendants can select items.

对于您的情况,我认为 ListView GridView 将适合.当用户单击该行中的控件时,事件将冒泡到ListView并且该项目将被选中.您可以覆盖默认样式,以使其不会显示为所选行: WPF ListView关闭选择.

For your scenario, I think a ListView with GridView would fit. When the user would click a control in the line, the event would bubble to the ListView and the item would get selected. You can override the default styles so it wouldn't display as selected line: WPF ListView turn off selection.

这篇关于在ItemsControl中获取选定的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆