Get DataContext from TextBlock MouseDown

前端 未结 2 375
攒了一身酷
攒了一身酷 2021-01-03 11:57

Bound column in a ListView GridView

How to get the DataSource in a MouseDown event


               


        
相关标签:
2条回答
  • 2021-01-03 12:34

    try this...

    private void NameCol_mousedown(object sender, MouseButtonEventArgs e)
    {
        var tb = (TextBlock)e.OriginalSource;
        var dataCxtx = tb.DataContext;
        var dataSource = (DocProp)dataCxtx;
    }
    
    0 讨论(0)
  • 2021-01-03 12:40

    For anyone still having problems, this works fine as well.

    private void MenuItem_Click(object sender, RoutedEventArgs e)
    {
        var baseobj = sender as FrameworkElement;
        var myObject = baseobj.DataContext as (Object you expect);
    
        // Example: var myObject = baseobj.DataContext as Dog;
        // myObject.Bark();
    }
    
    0 讨论(0)
提交回复
热议问题