绑定到WPF组合框

时间:2017-01-23 19:52:12

标签: c# wpf combobox

我有一个模型类,我希望绑定一个组合框。我的计划是拥有一个有两个属性的对象。 1)一个ObservableCollection,它包含我想用它填充组合框的项目。 2)一个字符串属性,用于存储所选项的值。我似乎无法让这个工作,并接受建议。我试图尽可能地遵循MVVM。我观察到的行为是一个空的组合框。

班级看起来像这样。

    public class WellListGroup : Notifier
{

    private ObservableCollection<string> _headers;

    public ObservableCollection<string> headers
    {
        get { return this._headers; }
        set { this._headers = value; OnPropertyChanged("headers"); }
    }

    private string _selected;

    public string selected
    {
        get { return this._selected;}
        set { this._selected = value; OnPropertyChanged("selected");}
    }

}

Notifier看起来像:

public class Notifier : INotifyPropertyChanged
{

    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged(string propertyName)
    {
        if(PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }


}

}

我的viewmodel调用数据访问层,创建我希望绑定的以下对象。

public class MainViewModel :  Notifier
{
    public static getWells gw = new getWells();
    public static ObservableCollection<string> headers = gw.getHeaders();


    public WellListGroup wlg = new WellListGroup {headers = headers, selected = null};


}

数据访问层 - getHeaders()

public ObservableCollection<string> getHeaders()
{
    ObservableCollection<string> vals = new ObservableCollection<string>();
    WVWellModel wvm = new WVWellModel();

    var properties = getProperties(wvm);

    foreach (var p in properties)
    {
        string name = p.Name;
        vals.Add(name);
    }


    return vals;

}

然后是观点:

<ComboBox DockPanel.Dock="Top" ItemsSource = "{Binding Path = wlg.headers}" SelectedItem="{Binding Path = wlg.selected}"></ComboBox>

查看代码(设置数据上下文的位置)

public partial class MainView : Window
    {
        public MainView()
        {
            InitializeComponent();
            MainViewModel mvm = new MainViewModel();
            DataContext = mvm;
        }
    }

App.xaml.cs

public partial class App : Application
{
    private void OnStartup(object sender, StartupEventArgs e)
    {
        Views.MainView view = new Views.MainView();
        view.Show();
}

private void APP_DispatcherUnhandledException(object sender,DispatcherUnhandledExceptionEventArgs e)
{
    MessageBox.Show(e.Exception.Message);
    e.Handled = true;
}

}

我已经尝试过几次这样的迭代,但是因为我的生活无法实现这一点。我看到一个空的组合框。

1 个答案:

答案 0 :(得分:0)

我将假设DataContext在视图上设置为MainViewModel。

我认为你的名单小组应该致电OnPropertyChanged

public class MainViewModel :  Notifier
{
    public static getWells gw = new getWells();
    public static ObservableCollection<string> headers = gw.getHeaders();

    private WellListGroup _wlg = new WellListGroup {headers = headers, selected = null};
    public WellListGroup wlg 
    {
        get { return _wlg; }
        set { _wlg = value; OnPropertyChanged("wlg"); }
}

组合框绑定应如下所示:

<ComboBox 
    ItemsSource = "{Binding wlg.headers}" 
    SelectedItem = "{Binding wlg.selected Mode=TwoWay}"
/>

如果这些都不起作用,我会确保MainViewModel被实例化并分配给Page构造函数中的DataContext或页面加载的事件。

这是一个代码项目教程,可能有助于细分绑定过程Step by Step WPF Data Binding with Comboboxes