基本WPF绑定到组合框中的集合。 [英] Basic WPF binding to collection in combobox.

查看:84
本文介绍了基本WPF绑定到组合框中的集合。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做错了什么?

与Combobox ItemsSource的基本绑定。



我尝试了什么:



C#

  public   partial   class  MainWindow:Window 
{
ObservableCollection< string> _items = new ObservableCollection< string> { a1 a2};


public ObservableCollection< string>商品

{

获取 {返回 _items ; }

set {_items = value ; }

}
public MainWindow()
{
InitializeComponent();
var aa = a1;
}
}



XAML



 <   StackPanel  >  
< ComboBox 名称 = a1 ItemsSource = {Binding项目} >


< / ComboBox >
< / StackPanel >

解决方案

阅读另一个代码项目文章

与Comboboxes一步一步的WPF数据绑定 [ ^ ]



Quote:

ItemsSource - 绑定到我们在上面定义为App.xaml文件中的应用程序资源的静态资源数组'ColorListString'。此项列表用于填充ComboBox的Items集合。此集合用于生成显示在下拉列表中的项目。

DisplayMemberPath - 绑定到ItemsSource列表中ComboBoxItemString对象的ValueString属性。从下拉列表中选择一个项目时,这是显示给用户的值。

SelectedValuePath - 绑定到ComboBoxItemString对象的ValueString属性,在ItemsSource列表。从下拉列表中选择一个项目时,这是用于获取将SelectedItem值设置为的值的属性。

SelectedValue - 使用属性绑定绑定{Binding ColorString}。当在列表中选择一个项目时,这是数据对象上的属性,该属性设置为从SelectedValuePath属性返回的值。





< pre lang =xml> < pre > < combobox < span class =code-attribute> itemssource = {StaticResource ColorListString} < span class =code-keyword>>
DisplayMemberPath =ValueString
SelectedValuePath =ValueString
SelectedValue ={Binding ColorString}/> < / combobox >


这个答案是在发布后我跳的方式你现在已经弄明白了,但是当你在没有源的情况下进行绑定时,默认的源是DataContext属性。在此示例中,尚未设置DataContext属性。此示例中最简单的解决方法是执行此操作:



 public MainWindow()
{
InitializeComponent();
DataContext = this;
}





我个人不推荐视图是它自己的DataContext,但如果你要去要做到这一点,你也应该让它暴露属性你绑定到DependencyProperty。


你好。



你需要在viewmodel(带有observable集合的类)上实现INotifyPropertyChanged。



完成后,您将使用界面提供的事件处理程序,让您的GUI知道您的集合何时更新。



典型地看起来像这样。



公共类YourClass:INotifyPropertyChagned 
{
private ObservableCollection< string> _collectionOfStrings;
public ObservableCollection< string> CollectionOfStrings
{
get => _collectionOfStrings
set
{
if(value!= _collectionOfStrings)
{
_listOfStrings = value;
OnPropertyChagned(CollectionOfStrings);
}
}
}

公共事件PropertyChangedEventHandler PropertyChanged;

[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName =
null)
{
PropertyChanged?.Invoke(this,new PropertyChangedEventArgs (propertyName的));
}
}


What i'm doing wrong?
Basic binding to Combobox ItemsSource.

What I have tried:

C#

public partial class MainWindow : Window
    {
        ObservableCollection<string> _items = new ObservableCollection<string> { "a1", "a2" };


        public ObservableCollection<string> Items

        {

            get { return _items; }

            set { _items = value; }

        }
        public MainWindow()
        {
            InitializeComponent();
            var aa = a1;
        }
    }


XAML

<StackPanel>
       <ComboBox Name="a1" ItemsSource="{Binding Items}">


       </ComboBox>
   </StackPanel>

解决方案

read another codeproject article
Step by Step WPF Data Binding with Comboboxes[^]

Quote:

ItemsSource - is bound to the static resource array 'ColorListString' that we defined above as an application resource in the App.xaml file. This list of items is used to populate the Items collection of the ComboBox. This collection is used to generate the items that show up in the dropdown list.
DisplayMemberPath - is bound to the ValueString property of the ComboBoxItemString object, in the ItemsSource list. When an item is selected from the dropdown list, this is the value that is displayed to the user.
SelectedValuePath - is bound to ValueString property of the ComboBoxItemString object, in the ItemsSource list. When an item is selected from the dropdown list, this is the property used to get the value to set the SelectedItem value to.
SelectedValue - is bound using a property binding of "{Binding ColorString}". When an item is selected in the list, this is the property on the data object that is set to the value returned from the SelectedValuePath property.



<pre><combobox itemssource="{StaticResource ColorListString}">
            DisplayMemberPath="ValueString" 
            SelectedValuePath="ValueString" 
            SelectedValue="{Binding ColorString}" /></combobox>


This answer is way after the posting and I hope you figured it out by now, but when you're binding without a source the default source is the DataContext property. In this example your DataContext property has not been set yet. The easiest fix in this example is to do this:

public MainWindow()
{
    InitializeComponent();
    DataContext = this;
}



I personally do not recommend a view being it's own DataContext, but if you're going to do it you should also make it's exposed property you're binding to a DependencyProperty.


Hi.

You need to implement INotifyPropertyChanged on you viewmodel (class with your observable collection).

Once that is done you'll use the eventhandler provided by the interface to let your GUI know that whenever your collection is updated.

Typcally it looks like this.

public class YourClass : INotifyPropertyChagned
{
    private ObservableCollection<string> _collectionOfStrings;
    public ObservableCollection<string> CollectionOfStrings
    {
        get => _collectionOfStrings
        set
        {
            if (value != _collectionOfStrings)
            {
                _listOfStrings = value;
                OnPropertyChagned("CollectionOfStrings");
            }
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    [NotifyPropertyChangedInvocator]
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName =
        null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}


这篇关于基本WPF绑定到组合框中的集合。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆