如何在属性变化时更新可观察对象集合,并在每次变化时存储它



我有一个动态列表框,其中包含显示列表项的文本框,因此我可以编辑列表框项。我的应用程序设置文件包含我想为该列表框绑定的字符串集合。我还想在每次更改列表框项时更新设置文件,我创建了实现inotifypropertychanged的类。我已经将字符串集合从设置文件转换为具有字符串属性的自定义类型的可观察集合。我将文本框绑定到自定义类的属性,并在属性更改时更新源属性。我还想更新可观察对象集合。这也会更新我的应用设置文件。请帮帮我。任何帮助都会非常感激。我的代码:

public class WindowViewModel : INotifyPropertyChanged
{
    private ObservableCollection<UrlModel> customcollection;
    public ObservableCollection<UrlModel> CustomCollection
    {
        get { return customcollection; }
        set
        {
            customcollection = value;
            NotifyPropertyChanged("CustomCollection");
        }
    }
    public event PropertyChangedEventHandler PropertyChanged;
    protected void NotifyPropertyChanged(string property)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(property));
    }
    public WindowViewModel()
    {
        List<string> customlist = Properties.Settings.Default.CustomList.Cast<string>().ToList();
        List<UrlModel> urllist = new List<UrlModel>();
        urllist = customlist.Select(item => new UrlModel() { urlString = item }).ToList();
        CustomCollection = new ObservableCollection<UrlModel>(urllist);
    }
}
public class UrlModel : INotifyPropertyChanged
{
    private string url;
    public string urlString
    {
        get { return url; }
        set
        {
            url = value;
            NotifyPropertyChanged("urlString");
        }
    }
    public event PropertyChangedEventHandler PropertyChanged;
    protected void NotifyPropertyChanged(string property)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(property));
    }
}

/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        ViewModel = new WindowViewModel();
        ListTwo.ItemsSource = ViewModel.CustomCollection;
    }
    private WindowViewModel viewModel;
    public WindowViewModel ViewModel
    {
        get { return viewModel; }
        set{
            viewModel = value;
            DataContext = value;
        }
    }
}   

}

在插入ObservableCollection时为每个url添加属性更改的事件处理程序

public WindowViewModel()
{
    List<string> customlist = Properties.Settings.Default.CustomList.Cast<string>().ToList();
    List<UrlModel> urllist = new List<UrlModel>();
    urllist = customlist.Select(item => new UrlModel() { urlString = item }).ToList();
    CustomCollection = new ObservableCollection<UrlModel>(urllist);
    foreach(var model in CustomCollection)
    {
        model.PropertyChaged += SettingsUpdater; //Settings update fucntion
    }
}

SettingsUpdater的一个幼稚的实现只会在其中一个url发生变化时更新设置中的整个url列表

我相信你正在使用数据模板使你的列表框可编辑。如果是这种情况,在绑定文本时,在Xaml代码中包括text ="{binding urlString,UpdateSourceTrigger=PropertyChanged}"。

        <ListBox.ItemTemplate>
            <DataTemplate>
                <TextBox Name="EditableText" Text="{Binding urlString,UpdateSourceTrigger=PropertyChanged}"/>
            </DataTemplate>
        </ListBox.ItemTemplate>

相关内容

  • 没有找到相关文章

最新更新