Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# TreeViewItem未使用绑定项Source、ObservableCollection和INotifyPropertyChanged进行更新_C#_.net_Wpf_Xaml - Fatal编程技术网

C# TreeViewItem未使用绑定项Source、ObservableCollection和INotifyPropertyChanged进行更新

C# TreeViewItem未使用绑定项Source、ObservableCollection和INotifyPropertyChanged进行更新,c#,.net,wpf,xaml,C#,.net,Wpf,Xaml,我知道这个问题已经被问了很多次了,但即使尝试了所有不同的答案,我仍然无法让它对我起作用。我试图绑定到的对象正在代码中正确更新,因此唯一不起作用的是当ItemsSource更改时更新TreeViewItem的子项 看起来我把一切都安排好了,但也许是因为我把事情联系在一起,所以这一切都不起作用。我在Windows7的VS2015中使用C#.NET4.5WPF项目。我绑定到一个静态类的静态属性,该属性只有一个到TreeViewItem的ItemsSource和设置DisplayMemberPath的g

我知道这个问题已经被问了很多次了,但即使尝试了所有不同的答案,我仍然无法让它对我起作用。我试图绑定到的对象正在代码中正确更新,因此唯一不起作用的是当ItemsSource更改时更新TreeViewItem的子项

看起来我把一切都安排好了,但也许是因为我把事情联系在一起,所以这一切都不起作用。我在Windows7的VS2015中使用C#.NET4.5WPF项目。我绑定到一个静态类的静态属性,该属性只有一个到TreeViewItem的ItemsSource和设置DisplayMemberPath的get方法

XAML:

我希望能够在不必在代码隐藏中调用Refresh()的情况下实现这一点。我已经尝试直接绑定到Shared.Profiles,但是没有任何帮助。ColorProfile是从配置文件继承的基类

希望有一些愚蠢的,简单的东西我错过了。提前谢谢你的帮助

更新:


经过进一步检查,看起来ItemsSource甚至没有更新。在调试期间,我可以在控件的属性资源管理器中看到ItemsSource已绑定到ObservableCollection,但ItemsSource未反映对列表所做的更改。如果我手动绑定后面的代码,那么它就可以工作。

Notify是一个在其自身上叫喊PropertyChanged的程序

没有人绑定通知,因此没有人正在更新


需要实现INotifyPropertyChanged或从NotifyChanged继承的人是共享的。

此类问题通常是由于未调用重新绘制事件造成的。简单的解决方案是将ItemSource设置为null,然后返回到实际的源代码。如果有必要,我可以这样做。这就是我过去一直让它工作的方式,但是如果没有任何额外的代码也能让它工作,那就太好了,我希望我能!不幸的是,静态类不能从任何一个继承。我理解。因此,您可以共享继承DependencyObject,并将所有属性作为DependencyProperties。
<!-- 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 ...
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
}
// 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));
    }

}