Silverlight ItemsControl 行为:如何获取我单击的项目?

发布于 2024-10-17 16:35:49 字数 1275 浏览 9 评论 0 原文

我正在为 ItemsControl 创建一个行为,目的是选择我单击的项目(并将其添加到所选项目的列表中)。

所以很容易获得所有项目:

hours = AssociatedObject.ItemsSource as List<Hour>;

当然我可以写 hours[0].Selected = true;

但后来我有一个鼠标事件,我尝试写这样的东西:

void AssociatedObject_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        hour = sender as Hour;
    }

问题是,它没有像我预期的那样工作......发送者不是一个小时,它是一个 ItemsControl。

我没有任何迹象表明点击了哪个小时。 那么我应该怎么做才能获得时间呢?

编辑 我的代码的工作原理如下: 有一个 ItemsControl 绑定到 Days 列表。 每天都有一个时间列表。 为了表示这一点,有一个绑定到(天。)小时的内部 ItemControl。 为了代表每个小时,有一个边界。

看起来像这样:

 <ItemsControl x:Name="daysPanel" Grid.Column="1" ItemsSource="{Binding Days}">
       <ItemsControl.ItemTemplate>
            <DataTemplate>
                <ItemsControl x:Name="dayHours" ItemsSource="{Binding Hours}" Grid.Row="1">
                     <ItemsControl.ItemTemplate>
                            <DataTemplate>
                                  <Border Name="dayHourBorder" Tag="{Binding}" Height="30" BorderBrush="#B0B6BE" Width="193" BorderThickness="1,0,1,1" Background="{Binding Path=Selected, Converter={StaticResource boolToColorConverter}}" >

I'm creating a behavior for an ItemsControl with the purpose of selecting the item i click on (and adding it to a list of selected items).

So it's easy to get all the items:

hours = AssociatedObject.ItemsSource as List<Hour>;

and of course i could write hours[0].Selected = true;

but then I've got a mouse event, that i tried writing something like this:

void AssociatedObject_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        hour = sender as Hour;
    }

the problem is, it's not working like i expected... the sender is not an Hour, it's an ItemsControl.

and I've got no indication as to which hour was clicked.
so what should i do to get the hour?

Edit
My code works like this:
there's an ItemsControl bound to a list of Days.
each day has a list of hours.
and to represent that, there is an inner ItemControl bound to (day.)Hours.
and to represent each hour, there's a border.

looks like this:

 <ItemsControl x:Name="daysPanel" Grid.Column="1" ItemsSource="{Binding Days}">
       <ItemsControl.ItemTemplate>
            <DataTemplate>
                <ItemsControl x:Name="dayHours" ItemsSource="{Binding Hours}" Grid.Row="1">
                     <ItemsControl.ItemTemplate>
                            <DataTemplate>
                                  <Border Name="dayHourBorder" Tag="{Binding}" Height="30" BorderBrush="#B0B6BE" Width="193" BorderThickness="1,0,1,1" Background="{Binding Path=Selected, Converter={StaticResource boolToColorConverter}}" >

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

绿萝 2024-10-24 16:36:10

我必须做一些类似于你正在做的事情。我想让当前行成为选定的行。

最简单的方法是使用 ICollectionView MSND 链接 并将其绑定到 ItemsControl
然后,您可以添加一个行为(如果尚不存在)来侦听所选事件并相应地更改当前事件。

然后,您只需要在 ViewModel 上连接 CurrentChanged 事件,就可以与 UI 完全解耦:)

让我知道这是否是您正在寻找的内容,我可以尝试获取一些以我的代码为例。

I had to do something similar to what you're doing. I wanted to make the current row the selected one.

The easiest way is to use an ICollectionView MSND link and bind THAT to the ItemsControl.
You can then add a behaviour (if it's not there already) that listens to the selected event and changes current accordingly.

Then you only need to hook up the CurrentChanged event on your ViewModel and you're completely decoupled from the UI :)

Let me know if it's what you're looking after and I may try and get some of my code as an example.

美胚控场 2024-10-24 16:36:09

ItemsControl 本身不为当前选定的项目提供任何属性或事件。您必须使用一个类,例如 ListBox,派生自 ItemsControl,分别来自 选择器,因为它包含项目选择的功能(SelectedItem、SelectedIndex 属性,...)。

The ItemsControl by it self does not provide any properties or events for the currently selected item. You have to use a class, such as ListBox, that derived from ItemsControl, respectively from Selector, cause this contains the functionality for item selection (SelectedItem, SelectedIndex property,...).

入怼 2024-10-24 16:36:07

我相信这应该可行 - 发送者将是发送单击事件的 UI 元素,并且由于您使用 ItemsSource 来设置它,因此每个项目的 DataContext 将是您所追求的:

hour = (sender as FrameworkElement).DataContext as Hour

I believe this should work - the sender will be the UI element that sent the click event, and since you're using ItemsSource to set it up, each item's DataContext will be what you're after:

hour = (sender as FrameworkElement).DataContext as Hour
南七夏 2024-10-24 16:36:05

VisualTreeHelper 可能对您有用。
您可以使用鼠标单击的位置获取所有元素并获取边框。它的标签与 Hours 绑定,因此您可以获取它。
从 SO 和 VisualTreeHelper 来自 http://blogs.msdn.com 一定可以帮助您。

VisualTreeHelper may be useful for you.
You can use to get all elements at point, where mouse clicked and get your Border. Its tag is binded to Hours, so you can get it.
Get the ItemsControl of a DataTemplate from SO and VisualTreeHelper from http://blogs.msdn.com must help you.

ぽ尐不点ル 2024-10-24 16:36:02

感谢大家试图提供帮助,但我找到了正确的方法。
我知道必须有一种简单的方法来获取被单击的 UI 元素,必须有一个!

确实有!
您无需与发件人合作,只需执行以下操作:
e.OriginalSource

为我提供了边界(以及与其绑定的时间)。
所以它就像“简单”一样:

(e.OriginalSource as Border).DataContext as Hour

thanks everyone for trying to help, but i found the right way to do it.
i knew there had to be simple way to get the UI element that was clicked on, there just had to be one!

and there was!
instead of working with the sender, you just have to do:
e.OriginalSource

that got me the border (and the hour that's bounded to it).
so it's as "simple" as:

(e.OriginalSource as Border).DataContext as Hour
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文