简体   繁体   English

TreeViewItem不会使用绑定的ItemsSource,ObservableCollection和INotifyPropertyChanged更新

[英]TreeViewItem not updating with bound ItemsSource, ObservableCollection, and INotifyPropertyChanged

I know this question has been asked a lot but even after trying all of the different answers I still can't get this to work for me. 我知道这个问题已经问了很多,但是即使尝试了所有不同的答案,我仍然无法解决这个问题。 The object I'm trying to bind to is updating correctly in the code behind so the only thing that isn't working is the children of the TreeViewItem updating when the ItemsSource is changed. 我试图绑定的对象正在后面的代码中正确更新,因此,唯一的不起作用的是更改ItemsSource时TreeViewItem的子级更新。

It seems that I have everything set up correctly but maybe there is something about how I am tying things together that is making this not work. 看来我的所有设置都正确,但也许我在将事情捆绑在一起方面有些问题,这使它无法正常工作。 I am using C# .NET 4.5 WPF project in VS 2015 in Windows 7. I am binding to a static classes' static property that has only a get method to a TreeViewItem's ItemsSource and setting DisplayMemberPath. 我正在Windows 7中的VS 2015中使用C#.NET 4.5 WPF项目。我绑定到静态类的静态属性,该静态属性仅对TreeViewItem的ItemsSource具有get方法并设置DisplayMemberPath。

XAML: XAML:

<!-- Menu tree -->
        <TreeView Grid.Column="0"
                  x:Name="menutree"
                  Background="Transparent"
                  BorderThickness="0">
            <!-- Profiles TVI -->
            <TreeViewItem Header="{x:Static loc:Resources.profiles}"
                          IsExpanded="True">
                <!-- Color profile TVI -->
                <TreeViewItem x:Name="colorTvi"
                              Header="{x:Static loc:Resources.colorProfiles}"
                              MouseRightButtonDown="colorTvi_MouseRightButtonDown"
                              DisplayMemberPath="Name"
                              ItemsSource="{Binding Source={x:Static local:Shared.ColorProfiles}, Mode=OneWay}" />
                 <TreeViewItem ...

Class / Properties being bound to: 类/属性绑定到:

public static class Shared
{
    #region Getter / Setter

    // Notify property changed
    public static NotifyChanged Notify { get; set; } = new NotifyChanged();

    // All profiles that have been created
    public static List<Profile> Profiles
    {
        get { return _Profiles; }
        set
        {
            // Set profile
            _Profiles = value;
            Notify.OnPropertyChanged(nameof(Profiles));
            Notify.OnPropertyChanged(nameof(ColorProfiles));
        }
    }
    private static List<Profile> _Profiles = new List<Profile>();

    // Color profiles
    public static ObservableCollection<ColorProfile> ColorProfiles
    {
        get
        {
            return new ObservableCollection<ColorProfile>(
                Profiles?.Where(m => m.GetType() == typeof(ColorProfile))?.Cast<ColorProfile>()?.ToList() ??
                new List<ColorProfile>());
        }
    }

    #endregion
}

The NotifyChanged class: NotifyChanged类:

// Property changed class
public class NotifyChanged : INotifyPropertyChanged
{

    // Property changed event
    public event PropertyChangedEventHandler PropertyChanged;

    // Notify property changed
    public void OnPropertyChanged(string name)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
    }

}

I would like to get this to work without having to call Refresh() in code behind. 我想让它工作而不必在后面的代码中调用Refresh()。 I have tried binding to Shared.Profiles directly but that doesn't help any. 我已经尝试直接绑定到Shared.Profiles,但这无济于事。 ColorProfile is a base class that inherits from Profile. ColorProfile是从Profile继承的基类。

Hopefully there's some stupid, simple thing I'm missing. 希望我缺少一些愚蠢,简单的东西。 Thanks in advance for the help. 先谢谢您的帮助。

UPDATE : 更新:

On further inspection it actually looks like the ItemsSource isn't even updating. 经过进一步检查,实际上看起来ItemsSource甚至没有更新。 During debugging I can see in the control's property explorer that the ItemsSource is bound to an ObservableCollection but the ItemsSource is not reflecting the changes made to the list. 在调试期间,我可以在控件的属性浏览器中看到ItemsSource绑定到ObservableCollection,但是ItemsSource并未反映对列表所做的更改。 If I manually bind in the code behind then it works. 如果我手动绑定后面的代码,那么它将起作用。

Notify is the one shouting PropertyChanged, on itself. 通知本身就是一个大喊PropertyChanged的事件。

No one is binded to Notify so no one is updating. 没有人绑定到通知,因此也没有人在更新。

The one who needs to implement INotifyPropertyChanged, or inherit from NotifyChanged is Shared. 需要实现INotifyPropertyChanged或从NotifyChanged继承的人是共享的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ObservableCollection无法通过INotifyPropertyChanged更新 - ObservableCollection not updating through INotifyPropertyChanged 通过INotifyPropertyChanged更新ListView的ItemsSource - Updating ListView's ItemsSource via INotifyPropertyChanged 绑定到ObservableCollection的TextBox无法更新 - TextBox bound to ObservableCollection not updating 使用INotifyPropertyChanged更新ObservableCollection项属性 - Updating ObservableCollection Item properties using INotifyPropertyChanged 在没有INotifyPropertyChanged的情况下更新ObservableCollection成员属性的绑定 - Updating binding for ObservableCollection member property without INotifyPropertyChanged 虽然我实现了INotifyPropertyChanged,但ObservableCollection没有更新 - ObservableCollection not updating though I INotifyPropertyChanged is implemented 控制不立即使用INotifyPropertyChanged更新绑定属性 - Control not immediately updating bound property with INotifyPropertyChanged 从UI更改ObservableCollection而不更新Bound Collection - Changes to ObservableCollection from UI not updating the Bound Collection 绑定的ObservableCollection更改时,ListView不更新 - ListView not updating when the bound ObservableCollection changes INotifyPropertyChanged 和 ObservableCollection WPF - INotifyPropertyChanged and ObservableCollection WPF
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM