WPF ListView和ListBox等双击事件问题

上两篇文章中说双击行获取不到当前数据对象问题,

http://www.cnblogs.com/ligl/p/5636899.html

http://www.cnblogs.com/ligl/p/5629802.html

后来又研究发现可以从MouseButtonEventArgs参数中获取到

  <ListBox Grid.Row="1" ItemsSource="{Binding DataList}" 
                 MouseDoubleClick="ListBox_MouseDoubleClick"
                 SelectedItem="{Binding CurrentSelectItem}" Background="AliceBlue">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <DockPanel Height="50"  Background="DarkGray" Width="300">
                        <TextBox Text="{Binding Name}"  Height="30" Width="200" Background="DimGray"></TextBox>
                    </DockPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
View Code
 private void ListBox_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            var txtBox = e.Device.Target as TextBox;
            var model=txtBox.DataContext as ListBoxModel;
            ListBox listBox = sender as ListBox;
            if (listBox == null || listBox.SelectedItem == null)
            {
                MessageBox.Show("ListBox1双击对象为空...");
            }
        }
View Code

主要Code如下

  private void ListBox_MouseDoubleClick(object sender, MouseButtonEventArgs e){
var txtBox = e.Device.Target as TextBox; var model=txtBox.DataContext as ListBoxModel;//ListBoxModel是自定义的数据对象
}
原文地址:https://www.cnblogs.com/ligl/p/5639415.html