开发者

Silverlight ItemsControl behavior: how do i get an item i click on?

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, Conve开发者_运维知识库rter={StaticResource boolToColorConverter}}" >


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


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.


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


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,...).


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜