简体   繁体   中英

WPF, doesn't work OnPropertyChanged for Properties.Settings (StringCollection) in listView

I try to set content for listView from Properties.Settings (StringCollection). Contet set successful, but if i delete item, listView don't refresh. If i close and open SettingWindow, content inside listView is correct. It's mean, something wrong in DataBinding, probably doesn't work OnPropertyChanged.

SettingWindow.xaml:

<Window x:Class="FilmDbApp.Views.SettingWindow"
        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:p="clr-namespace:FilmDbApp.Properties"
        xmlns:local="clr-namespace:FilmDbApp.Views"
        mc:Ignorable="d"
        Title="Setting" Height="500" Width="400" ResizeMode="NoResize" WindowStartupLocation="CenterOwner">
    <DockPanel>
        <TabControl>
            <TabItem Header="Genre options">
                <StackPanel>
                    <ListView ItemsSource="{Binding Source={x:Static p:Settings.Default}, Path=Genres, UpdateSourceTrigger=PropertyChanged}" SelectedItem="{Binding SelectedGenre}"" VerticalAlignment="Top"/>
                    <Button Command="{Binding DeleteGenreCommand}" CommandParameter="{Binding SelectedGenre}" Content="Delete"/>
                </StackPanel>
            </TabItem>
            <TabItem Header="Autosave options"/>
        </TabControl>
    </DockPanel>
</Window>

SettingWindow.cs:

using System.Windows;
using FilmDbApp.ViewModels;

namespace FilmDbApp.Views
{
    /// <summary>
    /// Interaction logic for SettingWindow.xaml
    /// </summary>
    public partial class SettingWindow : Window
    {
        public SettingWindow()
        {
            InitializeComponent();

            DataContext = new SettingWindowViewModel();
        }
    }
}

SettingWindowViewModel.cs:

using System.ComponentModel;
using System.Runtime.CompilerServices;
using FilmDbApp.Views;
using FilmDbApp.Models;
using FilmDbApp.Utils;

namespace FilmDbApp.ViewModels
{
    class SettingWindowViewModel : INotifyPropertyChanged
    {
        private string selectedGenre;
        public string SelectedGenre
        {
            get { return selectedGenre; }

            set
            {
                selectedGenre = value;
                OnPropertyChanged("SelectedGenre");
            }
        }

        public SettingWindowViewModel()
        {

        }

        public event PropertyChangedEventHandler PropertyChanged;
        public void OnPropertyChanged([CallerMemberName] string prop = "")
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(prop));
            }
        }

        // Delete genre
        private RelayCommand deleteGenreCommand;
        public RelayCommand DeleteGenreCommand
        {
            get
            {
                return deleteGenreCommand ??
                    (deleteGenreCommand = new RelayCommand(obj =>
                    {
                        string genre = obj as string;
                        Properties.Settings.Default.Genres.Remove(genre);
                        Properties.Settings.Default.Save();
                        OnPropertyChanged("Genres");
                    }, (obj) => Properties.Settings.Default.Genres.Count > 0 && obj != null));
            }
        }
    }
}

Instead of binding to some other property in some other source you can utilize the power of ViewModel, which is used to work in between of view and models.

Add following property to a ViewModel

class SettingWindowViewModel : INotifyPropertyChanged
{
    public List<Genre> Genres => Properties.Settings.Default.Genres;
    ...
}

and bind to it

<ListView ItemsSource="{Binding Genres}"
          SelectedItem="{Binding SelectedGenre}"... />

Now inside various commands you should be able to tell bindings to update

OnPropertyChanged(nameof(Genres));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM