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是自定義的數據對象
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM