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}}" >
source share