请教:关于WPF中数据绑定TreeView 删除节点不刷新UI的问题
后台数据 几个关键的部分如下
/// <summary> ///ViewModel /// </summary> public class ForDictList:IMappingRule,INotifyPropertyChanged { public string DisplayeName { get; set; } public string Desc { get; set; } public string DictID { get; set; } public string CategoryID { get; set; } ForDictList _parent; public ForDictList(ForDictList Parent) { Children = new List<ForDictList>(); _parent = Parent; } public ForDictList Parent { get { return _parent; } set { _parent.OnPropertyChanged("Children"); _parent = value; } } List<ForDictList> _children; public List<ForDictList> Children { get { return _children; } set { _children = value; OnPropertyChanged("Children"); } } bool _isExpanded; public bool IsExpanded { get { return _isExpanded; } set { if (value != _isExpanded) { _isExpanded = value; OnPropertyChanged("IsExpanded"); if (_parent!=null&&!_parent._isExpanded) { _parent._isExpanded = true; } } } } bool _isSelected; public bool IsSelected { get { return _isSelected; } set { if (value != _isSelected) { _isSelected = value; OnPropertyChanged("IsSelected"); } } } public event PropertyChangedEventHandler PropertyChanged; void OnPropertyChanged(string name) { PropertyChangedEventHandler handler = this.PropertyChanged; if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(name)); } }
<TreeView Padding="0" Margin="0" BorderThickness="1"> <TreeView.ItemContainerStyle> <Style TargetType="{x:Type TreeViewItem}"> <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" /> <Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" /> <EventSetter Event="UIElement.PreviewMouseRightButtonDown" Handler="TreeViewItem_PreviewMouseRightButtonDown"/> </Style> </TreeView.ItemContainerStyle> <TreeView.ItemTemplate> <HierarchicalDataTemplate DataType="{x:Type local:ForDictList}" ItemsSource="{Binding Children,Mode=TwoWay}"> <StackPanel AllowDrop="True" Drop="StackPanel_Drop" MouseMove="StackPanel_MouseMove" Margin="0,0,0,10" Orientation="Horizontal"> <StackPanel.ContextMenu> <ContextMenu> </ContextMenu> </StackPanel.ContextMenu> <Image VerticalAlignment="Center" Source="{Binding Icon}" Width="30" Height="30" Margin="0,0,2,2"></Image> <TextBlock VerticalAlignment="Center" FontSize="12" Text="{Binding DisplayeName}"/> <!--<Image VerticalAlignment="Center" Source="{Binding EditIcon}" Margin="2,0,0,0"></Image>--> <!--<StackPanel.ToolTip> <TextBlock VerticalAlignment="Center" Text="{Binding Name}" TextWrapping="Wrap" MaxWidth="200" ></TextBlock> </StackPanel.ToolTip>--> </StackPanel> </HierarchicalDataTemplate> </TreeView.ItemTemplate> </TreeView>
ForDictList source_data= stackPanel.DataContext as ForDictList; ForDictList drag_data = e.Data.GetData(typeof(ForDictList)) as ForDictList;//在目标节点的children中加 入 拖动的节点 source_data.Children.Add(drag_data);//告诉拖动的节点的曾经的父节点 让其将该节点删除drag_data.Parent.Children.Remove(drag_data);