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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值