开发者

how to get value from list view on its double click event(WPF)

i have a problem guys i want to get the row detail when i click that row on my listview i know it how it perform in window form but it is different in WPF /i m using C# wpf please help me out thanks in Advance sh开发者_运维问答ashank


If you have a reference to the ListView, you can use the SelectedItem property. You could also bind SelectedItem to a property on your ViewModel and then read the value from there. Finally, you could set IsSynchronizedWithCurrentItem to True on your ListView and then use:

CollectionViewSource.GetDefaultView(sourceList).CurrentItem

where sourceList is the ItemsSource of the ListView.


You can add an event handler to your ListViewItems by adding the following XAML to your <Window.Resources>:

<Style TargetType="ListViewItem">
        <EventSetter Event="MouseDoubleClick" Handler="MyEventHandler" />
</Style>

Then you must add an event handler in your code:

 private void MyEventHandler(object sender, MouseButtonEventArgs e)
    {
        ListViewItem MyListViewItem = (ListViewItem)sender;
        MyClass MyObject = (MyClass)Item.Content;

        e.Handled = true;
    }

In this example, MyObject contains the object that is bound to the ListViewItem that was clicked on. For example, in one of my projects, I have a ListView that is bound to an ObservableCollection<SongData>. I would then use the following code:

  private void MyEventHandler(object sender, MouseButtonEventArgs e)
    {
        ListViewItem Item = (ListViewItem)sender;
        SongData Song = (SongData)Item.Content;

        // Example
        MessageBox.Show(Song.Title + " by " + Song.Artist);

        e.Handled = true;
    }

I don't know if this gets you any further, but it should work fine.

Oh, and by the way, you could also attach an eventhandler to the ListView use the SelectedItem property, but for me that resulted in some problems, for example, if you click on a columnheader or a blank space while an item is selected, it also triggers the eventhandler. Therefore, I'd rather use my first proposed approach.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜