0

我正在开发一个小型 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?

4

2 回答 2

2

您必须实施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.

定义一个新类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();
} 
于 2013-01-30T17:14:34.640 回答
0

您可能缺少 WPF 控件数据上下文基础知识。如果您使用 {Binding CurrentMolecule.FormulasList} 之类的绑定或父控件 datacontext 在您交换该项目的 DataContext 时绑定到“CurrentMolecule”,则绑定将重置。如果您希望即使在父数据上下文发生更改时也将数据上下文绑定到 FormulasList。您需要将该上下文直接绑定到 FormulasList 属性,并确保其他父控件不使用 CurrentMolecule 作为数据上下文,即使它的实例在您的 ViewModel 中发生更改。

于 2013-01-30T17:13:47.750 回答