[英]databinding to changing element property
我正在開發一個小型WPF應用程序,其中我有一個組合框,它與后面的代碼中的ObservableCollection綁定:
public Molecule CurrentMolecule { get; set; }
public ObservableCollection<string> Formulas { get; set; }
public MainWindow()
{
CurrentMolecule = new Molecule();
Formulas = new ObservableCollection<string>(CurrentMolecule.FormulasList.ToList());
DataContext = this;
InitializeComponent();
}
<ComboBox x:Name="cmbFormula" ItemsSource="{Binding Path=Formulas}" SelectionChanged="cmbFormula_SelectionChanged"/>
這適用於使用CurrentMolecule.FormulasList
填充我的組合框,但是如果在某些時候我將CurrentMolecule
設置為Molecule的新實例,則數據綁定不再有效。 我是否需要實現某種OnPropertyChanged事件,以便無論組合框的內容與CurrentMolecule.FormulasList
保持CurrentMolecule.FormulasList
?
您必須實現INotifyPropertyChanged
,然后才會在UI中更新更改。
以下是我對您的代碼所做的修改。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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;
using System.ComponentModel;
using System.Collections.ObjectModel;
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window, INotifyPropertyChanged
{
private Molecule _CurrentMolecule;
public Molecule CurrentMolecule
{
get
{
return _CurrentMolecule;
}
set
{
_CurrentMolecule = value;
OnPropertyChanged("CurrentMolecule");
Formulas = new ObservableCollection<string>(CurrentMolecule.FormulasList.ToList());
}
}
private ObservableCollection<string> _Formulas;
public ObservableCollection<string> Formulas
{
get { return _Formulas; }
set
{
_Formulas = value;
OnPropertyChanged("Formulas");
}
}
public MainWindow()
{
InitializeComponent();
CurrentMolecule = new Molecule();
DataContext = this;
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}
編輯:更好的方法是創建一個ViewModel,然后將其綁定到Window
的DataContext。
定義一個名為ViewModel
的新類,如下所示。 請注意,您可能希望更改命名空間
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Collections.ObjectModel;
namespace WpfApplication1
{
public class ViewModel : INotifyPropertyChanged
{
#region Properties
private Molecule _CurrentMolecule;
public Molecule CurrentMolecule
{
get
{
return _CurrentMolecule;
}
set
{
_CurrentMolecule = value;
OnPropertyChanged("CurrentMolecule");
Formulas = new ObservableCollection<string>(CurrentMolecule.FormulasList.ToList());
}
}
private ObservableCollection<string> _Formulas;
public ObservableCollection<string> Formulas
{
get { return _Formulas; }
set
{
_Formulas = value;
OnPropertyChanged("Formulas");
}
}
#endregion
#region Constructor
public ViewModel()
{
CurrentMolecule = new Molecule();
}
#endregion
#region INotifyPropertyChanged implementation
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
#endregion
}
}
修改后面文件的MainWindow
代碼,如下所示
public MainWindow()
{
InitializeComponent();
DataContext = new ViewModel();
}
您可能缺少WPF控件datacontext基礎知識。 如果您使用{Binding CurrentMolecule.FormulasList}之類的綁定或父控件,只要交換該項的DataContext,datacontext就會綁定到“CurrentMolecule”,綁定將重置。 如果您希望將datacontext綁定到FormulasList,即使父datacontext更改也是如此。 您需要將該上下文直接綁定到FormulasList屬性,並確保其他父控件不使用CurrentMolecule作為datacontext,即使其實例在ViewModel中更改也是如此。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.