WPF 树结构节点重命名

2017-12-09 18:46:59来源:CSDN作者:barry_hui人点击

分享
这里采用的是DecExpress的<dxg:TreeListControl>控件,主要思想是节点模板原本为<TextBlock> 重命名时改为可编辑的<TextBox>,失去焦点时<TextBox>隐藏<TextBlock>显示且两个控件绑定的数据一致,前台代码如下:
  <dxg:TreeListControl Name ="treeCatalog" ItemsSource="{Binding ListWorkSpace}" Margin="0,0,0,0" MaxHeight="1200" MaxWidth="2000" SelectionChanged="treeCatalog_SelectionChanged" PreviewMouseDown="treeCatalog_PreviewMouseDown"> <dxg:TreeListControl.Columns>     <dxg:TreeListColumn FieldName="NodeImage" Visible="False" VisibleIndex="0"/>     <dxg:TreeListColumn FieldName="Name" Visible="True" VisibleIndex="1">         <dxg:TreeListColumn.CellTemplate>             <DataTemplate>                 <StackPanel>                     <TextBlock Height="22" Padding="3,2,0,0"                                                                                                   VerticalAlignment="Center"                                Text="{Binding Path=RowData.Row.Name}"                                   FontWeight="{Binding Path=RowData.Row.IsActive,Converter={StaticResource btf}}"                                FontSize="{Binding Path=RowData.Row.IsActive,Converter={StaticResource bts}}"                                 IsHitTestVisible="True" PreviewMouseRightButtonDown="TextBlock_PreviewMouseRightButtonDown"></TextBlock>                     <TextBox Height="22" BorderThickness="0"                               VerticalContentAlignment="Center"                               Padding="0" Margin="0"                                                                                              VerticalAlignment="Center"                                                                                             Text="{Binding Path=RowData.Row.Name}"                              FontWeight="{Binding Path=RowData.Row.IsActive,Converter={StaticResource btf}}"                              FontSize="{Binding Path=RowData.Row.IsActive,Converter={StaticResource bts}}"                               IsHitTestVisible="True"                               Visibility="Collapsed" LostFocus="TextBox_LostFocus" KeyDown="TextBox_KeyDown"/>                                                                      </StackPanel>                                                               </DataTemplate>         </dxg:TreeListColumn.CellTemplate>     </dxg:TreeListColumn> </dxg:TreeListControl.Columns>                         </dxg:TreeListControl>
  后台代码  #region 重命名        TextBox tbox = null;        TextBlock tbl = null;        StackPanel SP = null;        private void rename_Click(object sender, RoutedEventArgs e)        {            tbox = FindVisualChild<TextBox>(SP);            tbl.Visibility = Visibility.Collapsed;            tbox.Visibility = Visibility.Visible;            tbox.Focus();            tbox.SelectAll();        }        private void TextBlock_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e)        {            tbl = (TextBlock)sender;            SP = GetParentObjectEx(tbl);        }        private void TextBox_LostFocus(object sender, RoutedEventArgs e)        {            tbl.Visibility = Visibility.Visible;            tbox.Visibility = Visibility.Collapsed;        }        private void TextBox_KeyDown(object sender, KeyEventArgs e)        {            if (e.Key == Key.Enter)            {                tbl.Visibility = Visibility.Visible;                tbox.Visibility = Visibility.Collapsed;            }        }        //获取视觉树父节点元素        public StackPanel GetParentObjectEx(DependencyObject obj)        {            DependencyObject parent = VisualTreeHelper.GetParent(obj);            while (parent != null)            {                if (parent is StackPanel)                {                    return (StackPanel)parent;                }                parent = VisualTreeHelper.GetParent(parent);            }            return null;        }        //获取视觉树子节点元素        private childItem FindVisualChild<childItem>(DependencyObject obj) where childItem : DependencyObject        {            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)            {                DependencyObject child = VisualTreeHelper.GetChild(obj, i);                if (child != null && child is childItem)                    return (childItem)child;                else                {                    childItem childOfChild = FindVisualChild<childItem>(child);                    if (childOfChild != null)                        return childOfChild;                }            }            return null;        }        #endregion

最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台