C# 使用WPF应用程序中更改的InotifyProperty

C# 使用WPF应用程序中更改的InotifyProperty,c#,wpf,.net-4.6.1,C#,Wpf,.net 4.6.1,我将.NET4.6.1与WPF应用程序一起使用。我们有一个observateCollection,我们希望将集合绑定到一个列表视图,该列表视图需要按字母顺序排序,并根据集合中的新项目自动更新 我使用CollectionViewSource并在StackOverflow上搜索此处进行更新,我需要使用INotifyPropertyChanged。但由于某种原因,它不起作用:( 对象类: using System.Text; using System.Threading.Tasks; namespa

我将.NET4.6.1与WPF应用程序一起使用。我们有一个
observateCollection
,我们希望将集合绑定到一个列表视图,该列表视图需要按字母顺序排序,并根据集合中的新项目自动更新

我使用
CollectionViewSource
并在StackOverflow上搜索此处进行更新,我需要使用
INotifyPropertyChanged
。但由于某种原因,它不起作用:(

对象类:

using System.Text;
using System.Threading.Tasks;

namespace ObservableCollection
{
    public class AlarmTypes : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        private string name; 
        public AlarmTypes(string _name)
        {
            this.name = _name;
        }

        public string Name
        {
            get { return name; }
            set
            {
                name = value;
                OnPropertyChanged("Name");

            }
        }

        protected void OnPropertyChanged(string prop)
        {
            PropertyChangedEventHandler handler = PropertyChanged;

            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(prop));
            }
        }
    }
}
XAML文件

<Window x:Class="ObservableCollection.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:ObservableCollection"
        xmlns:componentModel="clr-namespace:System.ComponentModel;assembly=WindowsBase"
        xmlns:clr="clr-namespace:System;assembly=mscorlib"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>


        <Grid.Resources>
            <CollectionViewSource x:Name="CollectionViewSource" x:Key="src" Source="{Binding alarmTypes }" IsLiveFilteringRequested="True">
                <CollectionViewSource.LiveSortingProperties>
                    <clr:String>Name</clr:String>
                </CollectionViewSource.LiveSortingProperties>
                <CollectionViewSource.SortDescriptions>
                    <componentModel:SortDescription PropertyName="Name" />
                </CollectionViewSource.SortDescriptions>
            </CollectionViewSource>
        </Grid.Resources>



        <ListView x:Name="lstAlarmTypes" HorizontalAlignment="Left" Height="319" Margin="585,48,0,0" VerticalAlignment="Top" Width="157" ItemsSource="{Binding Source={StaticResource src}}">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="Name"  DisplayMemberBinding="{Binding Name}" />
                </GridView>
            </ListView.View>
        </ListView>
        <TextBox HorizontalAlignment="Left" Height="23" Margin="10,48,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" Name="textBox1"/>
        <Button Content="Add item" HorizontalAlignment="Left" Margin="173,51,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>
        <ListView x:Name="LstNames2" HorizontalAlignment="Left" Height="301" Margin="339,66,0,0" VerticalAlignment="Top" Width="180">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="Name"  DisplayMemberBinding="{Binding Name}"/>
                </GridView>
            </ListView.View>
        </ListView>

    </Grid>
</Window>

我不知道绑定是否不起作用,或者它是属性,或者我还需要其他东西。第一个列表只有一个绑定到可观察的集合,并且在从文本框输入数据时起作用。但是第二个列表视图(包含collectionViewSource)显然不起作用。有什么想法吗?

您需要打开
>alarmTypes
转换为公共属性。

要求
alarmTypes
在窗口的DataContext中是公共属性。现在它是一个私有字段。将其设置为公共属性,设置
DataContext=this;
并删除直接的ItemsSource分配
LstNames2.ItemsSource=alarmTypes;
et DataContext=this;我没有得到任何LSINAMES2不重要,顺便说一句,这是collectionviewsource,但我似乎不能调用它。我在collectionviewsource上发现了microsoft自己的github,并且暂时能够解决一些问题。我已经修复了collectionviewsource,这意味着我可以在我的listview中看到已排序的项目。此外,我可以确认,当我添加一个条目并调试我的可观察的集合中有它的项目时,现在它肯定只更新问题HI。如果下面的答案解决了你的问题,请考虑将它标记为接受的答案。谢谢。
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace ObservableCollection
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private ObservableCollection<AlarmTypes> alarmTypes = new ObservableCollection<AlarmTypes>();
        private ObservableCollection<AlarmTypes> sortedTypes = new ObservableCollection<AlarmTypes>();


        public MainWindow()
        {
            InitializeComponent();
            LstNames2.ItemsSource = alarmTypes;

            var alarmType = new AlarmTypes("inbraak");            
            alarmTypes.Add(alarmType);

            var alarmType2 = new AlarmTypes("alarm");
            //alarmType.Name = textBox1.Text;
            alarmTypes.Add(alarmType2);

        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            var alarmType = new AlarmTypes(textBox1.Text);
            //alarmType.Name = textBox1.Text;
            alarmTypes.Add(alarmType);


        }
    }
}