(WPF)(MVVM) 我的 ListViewItems 仅在我更改视图时更新

(WPF)(MVVM) My ListViewItems are only updated when I change my view

我正在使用 MVVM 模型创建一个应用程序,在我的一个视图中,我有一个 ObservableCollection,我通过一个按钮创建了一个新元素,它出现在屏幕,问题是我有一个更新按钮,它更改了 ListViewItem 的名称,并且在我在视图之间切换之前此名称不会更改

问题

DNP3-Master 是我的项目,我激活的按钮将名称更改为“测试”,但直到我更改视图(这是一个用户控件)后它才会更新

MasterViwModel

 class MasterViewModel : ObservableObject
    {
        public ushort count { get; set; }
        public ObservableCollection<MasterTraceModel> MasterReference { get; set; }
        public RelayCommand CreateMaster { get; set; }
        public RelayCommand Update { get; set; }



        private ObservableCollection<MasterModel> _masterList;

        public ObservableCollection<MasterModel> MasterList
        {
            get { return _masterList; }
            set { _masterList = value; OnPropertyChanged(); }
        }



        private MasterModel _selectedMaster;//SelectedItem from ListView
        public MasterModel SelectedMaster
        {
            get { return _selectedMaster; }
            set { _selectedMaster = value; OnPropertyChanged(); }
        }

        public MasterViewModel()
        {


            MasterList = new ObservableCollection<MasterModel>();//my Observable Collections
            
            //Stuff

            this.count = 1;


            //Stuff

            CreateMaster = new RelayCommand(o =>
            {
                MasterList.Add(new MasterModel(this.count, "127.0.0.1", "20000", runtime));
                this.count = (ushort)(count + 1);
            });//Here I add the elements to my ObservableCollections

            //Stuff

            Update = new RelayCommand(o =>
            {
                SelectedMaster.SetName("Test");
            });
        }
    }

MasterView

<UserControl x:Class="Prototype.MVVM.View.MasterView"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:viewmodel="clr-namespace:Prototype.MVVM.ViewModel" 
            d:DataContext="{d:DesignInstance Type=viewmodel:MasterViewModel}"
            mc:Ignorable="d" 
            d:DesignHeight="450" d:DesignWidth="800">
   <Grid> 
                   <Border Margin="20,20,0,20" Background="#151515" CornerRadius="8">
                       <ListView Name="MasterListView" Margin="5"
                                 ItemsSource="{Binding MasterList}"
                                 SelectedItem="{Binding SelectedMaster}"
                                 ItemContainerStyle="{StaticResource MasterTheme}"
                                 Background="Transparent" 
                                 BorderThickness="0"
                                 />
                   </Border>

               <StackPanel Grid.Column="1" Margin="0,20,0,0">
                   <Button Margin="0,0,0,10" Grid.Column="1" Style="{StaticResource SmallBtn}" Command="{Binding Update}">
                       <Image Height="24" Width="24" Source="/Icons/cil-reload.png" RenderOptions.BitmapScalingMode="NearestNeighbor"/>
                   </Button>
               </StackPanel>
   </Grid>
</UserControl>

MasterModel

   class MasterModel : ObservableObject
   {
       public string Name { get; set; }
       public ushort Adress { get; set; }
       public string Host { get; set; }
       public string Port { get; set; }
       public Runtime _runtime { get; set; }
       public MasterChannel channel { get; set; }
       public ConnectStrategy CStrategy { get; set; }
       public string[] Delay { get; set; }

       public MasterModel(ushort Adress, string Host, string Port, Runtime runtime)
       {
           this.Name = "DNP3-Master-" + Adress.ToString();
           this.Adress = Adress;
           this.Host = Host;
           this.Port = Port;
           this._runtime = runtime;

           CStrategy = new ConnectStrategy();
           //CStrategy.MinConnectDelay = new TimeSp

           Delay = new string[3];
           Delay[0] = CStrategy.MinConnectDelay.ToString();
           Delay[1] = CStrategy.MaxConnectDelay.ToString();
           Delay[2] = CStrategy.ReconnectDelay.ToString();

           this.channel = MasterChannel.CreateTcpChannel(//Stuff);
       }

       public void SetName(string name)
       {
           this.Name = name;
       }
       
       public void Star(Runtime runtime)
       {
           Task.Run(async () =>
           {
               try
               {
                   await MasterFunctions.RunChannel(channel);
               }
               finally
               {
                   runtime.Shutdown();
               }
           });
       }

MasterModel class 应该实现 INotifyPropertyChanged 事件并在您调用 [=14] 时为数据绑定 属性 引发 PropertyChanged 事件=]:

private string _name;
public string Name
{
    get { return _name; }
    set { _name = value; OnPropertyChanged(); }
}

使用 ObservableCollection<T> 并不能取代实现 INotifyPropertyChanged 的需要,也不会为集合中的单个项目发出更改通知。它仅在项目添加到集合和从集合中删除时通知视图。