0

我有一个派生自 INotifyPropertyChanged 的​​类来跟踪属性更改。此属性绑定到 TreeView,并且需要在 TreeViewItems 中执行搜索。当“IsExpanded”冒泡到 TreeView 的根元素时,this.OnPropertyChanged("IsExpanded")由于某种原因调用了属性设置器。this.PropertyChanged == null为什么这里为 null 而在更深的 TreeView 元素上不为 null?我能用什么来解决这个问题?我的代码cs:

    public class TreeViewItemViewModel:INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

    ....................

        bool _isExpanded;
        public bool IsExpanded
        {
            get { return _isExpanded; }
            set
            {
                if (value != _isExpanded)
                {
                    _isExpanded = value;
                    this.OnPropertyChanged("IsExpanded");
                }

                //Expand all till the root
                if (_isExpanded && _parent != null)
                    _parent._isExpanded = true;

                //Lazy load children, if nessesary
                if (this.HasDummyChild)
                {
                    this.Children.Remove(DummyChild);
                    this.LoadChildren();
                }
            }

        }

        bool _isSelected;
        public bool IsSelected
        {
            get { return _isSelected; }
            set
            {
                if (value != _isSelected)
                {
                    _isSelected = value;
                    this.OnPropertyChanged("IsSelected");
                }
            }
        }
    .............................
    }

我的代码 XAML:

          <TreeView ItemsSource="{Binding Areas}">
                <TreeView.ItemContainerStyle>
                    <Style TargetType="{x:Type TreeViewItem}">
                        <Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" />
                        <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
                        <Setter Property="FontWeight" Value="Normal" />
                        <Style.Triggers>
                            <Trigger Property="IsSelected" Value="True">
                                <Setter Property="FontWeight" Value="Bold" />
                            </Trigger>
                        </Style.Triggers>
                    </Style>
                </TreeView.ItemContainerStyle>
                <TreeView.Resources>
                    <HierarchicalDataTemplate 
                      DataType="{x:Type vAreas:AreaViewModel}" 
                      ItemsSource="{Binding Children}"
                      >
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Text="{Binding Name}" />
                        </StackPanel>
                    </HierarchicalDataTemplate>
                </TreeView.Resources>
            </TreeView>
4

2 回答 2

0

问题是我从不订阅我的事件 PropertyChanged。一旦我订阅它,它就开始正常工作

this.PropertyChanged += AreaViewModel_PropertyChanged;

void AreaViewModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
    {
        if (e.PropertyName == "IsExpanded")
        {
            //Expand all till the root
            if (this.IsExpanded && this.Parent != null)
                this.Parent.IsExpanded = true;

            //Lazy load children, if nessesary
            if (this.HasDummyChild)
            {
                this.Children.Remove(DummyChild);
                this.LoadChildren();
            }
        }
    }
于 2013-05-24T05:21:42.007 回答
0

我与这个斗争了一段时间。最后结果证明,我的树视图集合是一个 ObservableCollection 属性,但实际上是一个列表。

一旦我把它变成一个 ObservableCollection 一切就开始工作了。

于 2014-08-22T13:08:12.733 回答