Inline editing TextBlock in a ListBox with Data Template (WPF)

前端 未结 4 811
耶瑟儿~
耶瑟儿~ 2020-12-13 16:39

Using WPF, I have a ListBox control with a DataTemplate inside it. The relevant XAML code is shown below:



        
4条回答
  •  孤城傲影
    2020-12-13 17:13

    If I may supplement, in order to cover the (double) part of the original question, in Youngjae's reply you make the following replacement in the xaml file:

    
    

    is replaced with

    
        
            
        
    
    

    adding also the proper RoutedCommand in UserControl.Resources

    
        
    
    

    and a CommandBinding in UserControl.CommandBindings

    
        
    
    

    Also in the code behind file:

    private void textBlockName_MouseDown(object sender, MouseButtonEventArgs e)
    {
        var grid = ((Grid) ((TextBlock) sender).Parent);
        var tbx = (TextBox)grid.Children[1];
        ((TextBlock)sender).Visibility = Visibility.Collapsed;
        tbx.Visibility = Visibility.Visible;
        this.Dispatcher.BeginInvoke((Action)(() => Keyboard.Focus(tbx)), DispatcherPriority.Render);
    }
    

    is replaced by

    private void CmdEditTextblock_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            var grid = ((Grid)((TextBlock)e.OriginalSource).Parent);
            var tbx = (TextBox)grid.Children[1];
            ((TextBlock)e.OriginalSource).Visibility = Visibility.Collapsed;
            tbx.Visibility = Visibility.Visible;
            this.Dispatcher.BeginInvoke((Action)(() => Keyboard.Focus(tbx)), DispatcherPriority.Render);
        }
    

    In case some people want to have left doubleclick as input gesture, like I did...

提交回复
热议问题