首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当PropertyChanged调用event PropertyChanged=null时

当PropertyChanged调用event PropertyChanged=null时
EN

Stack Overflow用户
提问于 2013-05-21 20:35:50
回答 2查看 1.6K关注 0票数 0

我有一个从INotifyPropertyChanged派生的类来跟踪属性更改。这些属性被绑定到TreeView,并且是在TreeViewItems中执行搜索所必需的。当在属性setters中时,当"IsExpanded“冒泡到TreeView的根元素时,出于某种原因,this.OnPropertyChanged("IsExpanded")被调用为this.PropertyChanged == null。为什么它在这里是null,在更深层的TreeView元素上不是null?我能用什么来解决这个问题呢?我的代码cs:

代码语言:javascript
复制
    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:

代码语言:javascript
复制
          <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>
EN

回答 2

Stack Overflow用户

发布于 2013-05-24 13:21:42

问题是我从来没有订阅过我的event PropertyChanged。一旦我订阅了它,它就开始正常工作了

代码语言:javascript
复制
this.PropertyChanged += AreaViewModel_PropertyChanged;

代码语言:javascript
复制
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();
            }
        }
    }
票数 0
EN

Stack Overflow用户

发布于 2014-08-22 21:08:13

我在这个问题上挣扎了一段时间。最后,我的集合提供给树视图的结果是一个ObservableCollection属性,但实际上是一个列表。

当我把它变成ObservableCollection的时候,一切都开始工作了。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16670156

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档