Reputation: 1301
I created a ListBox with ListBoxItems and add an MouseDown event handler to each of the ListBoxItems. The ListBoxItems are shown, but when I click on a ListBoxItem, the Event doesn't get fired.
How i set the MouseUp:
TrackedProcessList.ItemsSource = null;
TrackedProcessList.ItemsSource = this.tracks;
/*... some other code that doesn't matter ... */
ListBoxItem[] items = new ListBoxItem[TrackedProcessList.Items.Count];
for (int i = 0; i < TrackedProcessList.Items.Count; i++)
{
Object obj = TrackedProcessList.Items.GetItemAt(i);
//TrackedProcessList.UpdateLayout();
ListBoxItem item = (ListBoxItem)(TrackedProcessList.ItemContainerGenerator.ContainerFromIndex(i));
if (item != null)
{
item.MouseUp += new MouseButtonEventHandler(ListBoxItem_MouseUp_PostQuestion);
items[i] = item;
}
}
The Method which should be called (but it isn't):
private void ListBoxItem_MouseUp_PostQuestion(object sender, EventArgs e)
{
MessageBox.Show("ListBoxItem_MouseUp_fired");
}
My XAML:
<ListBox x:Name="TrackedProcessList" Height="145" Width="605" ItemsSource="{Binding}" BorderThickness="1,0" IsSynchronizedWithCurrentItem="True">
<DataTemplate>
<TextBlock MouseDown="ListBoxItem_MouseUp_PostQuestion" Text="{Binding Path=programName}" HorizontalAlignment="Stretch" ></TextBlock>
</DataTemplate>
</ListBox>
Do you have any ideas where the failure could be? There is no error. The Event just seems to be not bound to the ListBoxItem.
Upvotes: 0
Views: 1338
Reputation: 329
Use the ContainedControl property and set your events :)
kryptonListBox1.ContainedControl.MouseDown += kryptonListBox1_MouseDown_1;
Upvotes: 0
Reputation: 1
void OnListBox_Mouse_Down(object sender, MouseButtonEventArgs e)
{
e.Handled
}
void OnListBox_Mouse_Up(object sender, MouseButtonEventArgs e)
{
"Do Something";
}
Upvotes: 0
Reputation: 33384
It is because ListBoxItem
already handles both left and right click which means your event handler will not be triggered according to WPF routed event rules. Your either have to assign PreviewMouseDown
event or add event handler for handled events:
lbi.AddHandler(ListBoxItem.MouseDownEvent, new MouseButtonEventHandler(MouseEvent), true);
Upvotes: 2