Home > Back-end >  (WPF)(MVVM) My ListViewItems are only updated when I change my view
(WPF)(MVVM) My ListViewItems are only updated when I change my view

Time:10-09

I am creating an application with an MVVM model, in one of my views I have an ObservableCollection where by means of a button I create a new element and it appears on the screen, the problem is that I have a button to update that changes the name of the ListViewItem , and this name doesn't change until I switch between views

Problem ListView

The DNP3-Master are my Items and the button I activate changes the name to "Test" but it is not updated until I change my view (this is a UserControl)

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();
               }
           });
       }

CodePudding user response:

The MasterModel class should implement the INotifyPropertyChanged event and raise the PropertyChanged event for the data-bound property when you call SetName:

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

Using an ObservableCollection<T> doesn't replace the need to implement INotifyPropertyChanged and raise change notifications for the individual items in the collection. It notifies the view when items are added to and removed from the collection only.

  • Related