我创建了ListBoxItems一个ListBox并添加MouseDown事件处理程序,以每个ListBoxItems的。显示ListBoxItems,但是当我点击ListBoxItem时,事件不会被触发。ListBoxItem的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;
}
}
应该被调用的方法(但不是):
private void ListBoxItem_MouseUp_PostQuestion(object sender, EventArgs e)
{
MessageBox.Show("ListBoxItem_MouseUp_fired");
}
我的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>
你有什么想法,失败可能是?没有错误。该事件似乎没有绑定到ListBoxItem。
就像错字马克'item.MouseDown'应该是'item.MouseUp' – WiiMaxx
你的权利。我装了它;-) – Laokoon
你不能使用ListBox的'SelectionChanged'事件吗? – Jehof