簡體   English   中英

ListBoxItem MouseUp事件不會觸發

[英]ListBoxItem MouseUp Event doesnt fire

我用ListBoxItems創建了一個ListBox,並將MouseDown事件處理程序添加到每個ListBoxItems。 顯示了ListBoxItems,但是當我單擊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。

這是因為ListBoxItem已經處理了左鍵單擊和右鍵單擊,這意味着將不會根據WPF路由事件規則觸發事件處理程序。 您必須分配PreviewMouseDown事件或為已處理事件添加事件處理程序:

lbi.AddHandler(ListBoxItem.MouseDownEvent, new MouseButtonEventHandler(MouseEvent), true);
void OnListBox_Mouse_Down(object sender, MouseButtonEventArgs e)
{
   e.Handled
}

void OnListBox_Mouse_Up(object sender, MouseButtonEventArgs e)
{
   "Do Something";
}

使用ContainedControl屬性並設置事件:)

kryptonListBox1.ContainedControl.MouseDown += kryptonListBox1_MouseDown_1;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM