Silverlight ItemsControl elements: how to get the element that I click on?

I am creating a behavior for ItemsControl in order to select the item that I click on (and add it to the list of selected items).

So easy to get all items:

hours = AssociatedObject.ItemsSource as List<Hour>; 

and of course I could write a clock [0]. Selected = true;

but then I have a mouse event that I tried to write something like this:

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

the problem is that it does not work, as I expected ... the sender is not an Hour, it is an ItemsControl control.

and I have no indication of what time was pressed. so what should i do to get an hour?

Edit My code works as follows: there ItemControl is attached to the list of days. every day has a list of hours. and to represent this, there is an internal ItemControl associated with the (day.) Clock. and present every hour, there is a boundary.

looks like that:

  <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}}" > 
+4
source share
5 answers

Thanks everyone for the help, but I found the right way to do this. I knew that there should be an easy way to get the user interface element that I clicked on, there just had to be one!

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

which gave me the border (and the hour that limited it). therefore it is as "simple" as:

 (e.OriginalSource as Border).DataContext as Hour 
+8
source

VisualTreeHelper may be useful to you. You can use to get all the elements at the point where you clicked the mouse and got your border. Its tag is tied to a watch, so you can get it.
Getting the ItemsControl DataTemplate from SO and VisualTreeHelper from http://blogs.msdn.com should help you.

+1
source

I believe this should work - the sender will be the user interface element that sent the click event, and since you use the ItemSource to configure it, each DataContext element will be what you will be:

 hour = (sender as FrameworkElement).DataContext as Hour 
0
source

The ItemsControl element itself does not provide any properties or events for the currently selected item. You should use a class, for example, a ListBox obtained from ItemsControl, respectively from Selector , because it contains functions for selecting an item (SelectedItem, property SelectedIndex, ...).

0
source

I needed to do something similar to what you are doing. I wanted to make the current row selected.

The easiest way is to use the ICollectionView MSND link and bind it to the ItemsControl . Then you can add a behavior (if it does not already exist) that listens to the selected event and changes the current accordingly.

Then you only need to hook the CurrentChanged event on your ViewModel and you are completely separate from the user interface :)

Let me know if this is what you care, and I can try and get some of my code as an example.

0
source

Source: https://habr.com/ru/post/1340123/


All Articles