简体   繁体   English

C#WPF MVVM ComboBox绑定

[英]C# WPF MVVM ComboBox Binding

First of all, what I'm trying to do is a "simple" binding of a ComboBox to my source. 首先,我想做的是将ComboBox绑定到我的源代码上的“简单”。

The structure is something like: 结构类似于:

public class Data
{
    public ObservableList<string> List {get;set;}
    public string Selected {get;set;}
}

Also, it implements INotifyPropertyChanged interface. 此外,它实现INotifyPropertyChanged接口。 My problem is, i found several solutions to do this via XAML, unfortunately i can't do it with XAML since my ComboBoxes have to be generated during runtime. 我的问题是,我找到了几种通过XAML进行此操作的解决方案,但是不幸的是,我无法使用XAML进行此操作,因为必须在运行时生成ComboBoxes

So my question is, how i can bind my ComboBox to Data.List , and also the selected item (value?) to Data.Selected, and this one should be TwoWay so my Data class knows that something was selected. 所以我的问题是,我如何将我的ComboBox绑定到Data.List ,还将所选的项(值)绑定到Data.Selected,而这个应该是TwoWay这样我的Data类就知道选择了什么。 Keep in mind this has to be through c# code (XAML is no option unfortunately). 请记住,这必须通过c#代码进行(不幸的是,XAML不能选择)。

Thanks in advance. 提前致谢。 :) :)

It's pretty easy. 很简单 Assuming, that Data has properties instead of fields: 假设该Data具有属性而不是字段:

public class Data
{
    public Data()
    {
        List = new ObservableCollection<string>
        {
            "Apple", "Orange", "Lime"
        };
    }

    public ObservableCollection<string> List { get; private set; }
    public string Selected { get; set; }
}

you can write this: 你可以这样写:

var comboBox = new ComboBox
{
    DataContext = new Data()
};

comboBox.SetBinding(ComboBox.ItemsSourceProperty, new Binding("List"));
comboBox.SetBinding(ComboBox.SelectedItemProperty, new Binding("Selected") 
{ 
    Mode = BindingMode.TwoWay
});

To add ComboBox into visual tree, just call proper method for the container. 要将ComboBox添加到可视树中,只需为容器调用适当的方法即可。 Eg, this will work with any ContentControl (like Window ): 例如,这将适用于任何ContentControl (例如Window ):

AddChild(comboBox);

how i can bind my combobox to Data.List, and also the selected item (value?) 我如何将我的组合框绑定到Data.List,以及选定的项目(值?)

Create a custom composite user control which contains the combobox. 创建一个包含组合框的自定义复合用户控件。 Map the combobox's properties to two dependencies properties created on the custom control, one to load the data and the other to provide an on demand selected item's data. 将组合框的属性映射到在自定义控件上创建的两个依赖项属性,一个用于加载数据,另一个用于提供按需选择的项目的数据。 Any plumbing needs are done inside the codebehind which ultimately provides all the magic. 任何管道需求都在背后的代码内完成,最终提供了所有魔力。

Then you can create/bind this control dynamically in codebehind as needed in the other page you are working on. 然后,您可以根据需要在正在处理的另一页中的代码背后动态创建/绑定此控件。

Sounds like a sort of "recursive binding". 听起来像是一种“递归绑定”。 If your combos are in a container control, what you need is bound the container to a collection of your single combo model, so each view in the ItemsControl will be bound to a single combo model. 如果您的组合位于容器控件中,则需要将容器绑定到单个组合模型的集合,因此ItemsControl中的每个视图都将绑定到单个组合模型。

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

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