简体   繁体   English

绑定到WPF组合框

[英]Binding to WPF Combo Box

I have a model class that I wish to bind a combo box to. 我有一个模型类,希望将组合框绑定到该模型类。 My plan was to have an object with two propertied. 我的计划是让一个带有两个属性的对象。 1) an ObservableCollection that contains the items I want to populate the combo box with. 1)一个ObservableCollection,其中包含我要填充组合框的项目。 2) A string property that stores the value of the selected item. 2)一个字符串属性,用于存储所选项目的值。 I cannot seem to get this to work and open to suggestions. 我似乎无法使它起作用并接受建议。 I am Trying to follow MVVM as best as possible. 我正在尝试尽可能地遵循MVVM。 The behavior I observe is an empty combo box. 我观察到的行为是一个空的组合框。

The class looks like this. 该类看起来像这样。

    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 looks like: 通知程序如下所示:

public class Notifier : INotifyPropertyChanged
{

    public event PropertyChangedEventHandler PropertyChanged;

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


}

} }

And my viewmodel makes a call to a data access layer that creates the following object i wish to bind to. 我的视图模型调用了数据访问层,该数据访问层创建了我希望绑定到的以下对象。

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};


}

Data Access Layer - getHeaders() 数据访问层-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;

}

Then the view: 然后查看:

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

View Code Behind (Where the Data Context is set) 查看背后的代码(已设置数据上下文)

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

App.xaml.cs 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;
}

} }

I have tried several iterations of this but cant for the life of me get this to work. 我已经尝试过多次迭代,但是在我的一生中无法做到这一点。 I am presented with an empty combo box. 我看到一个空的组合框。

I am going to assume DataContext is set to MainViewModel on the view. 我将假定DataContext在视图上设置为MainViewModel。

I think you well list group should call OnPropertyChanged 我认为您很好的列表组应该调用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"); }
}

The combo box binding should look like this: 组合框绑定应如下所示:

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

If neither of those work I would make sure the MainViewModel is being instantiated and assigned to DataContext in the Page constructor or a page loaded event. 如果这些都不起作用,我将确保在页面构造函数或页面加载事件中实例化MainViewModel并将其分配给DataContext。

Here is a code project Tutorial that may help break down the binding process Step by Step WPF Data Binding with Comboboxes 这是一个代码项目教程,可以帮助您分解绑定过程分步使用组合框进行WPF数据绑定

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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