WPF数据绑定到组合框

WPF数据绑定到组合框

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

问题描述

我是WPF,C#和数据绑定的新手。我只是试图将一个简单的组合框绑定到一个ObservedCollection。这是代码:

  public class Directory 
{
private string _ikey;
public string IKey
{
get
{
return _ikey;
}
set
{
_ikey = value;
}
}
private string _ivalue;
public string IValue
{
get
{
return _ivalue;
}
set
{
_ivalue = value;
}
}

}
///< summary>
/// MainWindow.xaml的交互逻辑
///< / summary>
public partial class MainWindow:Window

{
public ObservableCollection< Directory> DirectoryList = new ObservableCollection< Directory>();

public MainWindow()
{
InitializeComponent();

DirectoryList = new ObservableCollection< Directory>();
目录_dirtemp = new Directory();
_dirtemp.IKey =1;
_dirtemp.IValue =史蒂夫;
DirectoryList.Add(_dirtemp);

_dirtemp = new Directory();
_dirtemp.IKey =2;
_dirtemp.IValue =John;
DirectoryList.Add(_dirtemp);




}
}

我的xaml看起来像这样:

 < Window x:Class =DataBindCombo.MainWindow
xmlns =http://schemas.microsoft.com/winfx/2006/xaml/presentation
xmlns:x =http://schemas.microsoft.com/winfx/2006/xaml
xmlns:local =clr-namespace:DataBindCombo
Title =MainWindowHeight =350Width =525>
< Grid>
< ComboBox Height =48Horizo​​ntalAlignment =LeftMargin =70,104,0,0Name =comboBox1VerticalAlignment =TopWidth =310
ItemsSource ={Binding Path = DirectoryList}
DisplayMemberPath =IValue
SelectedValuePath =IKey
>

看起来应该很简单。我能够把代码放在后面,它工作正常,但我想通过xaml绑定。



任何想法? TIA

解决方案

有几种方法可以解决这个问题。您需要使基础知识使XAML可以看到您的收藏。您可以通过将其设置为DataContext以隐式方式执行此操作。如果这是唯一你绑定的东西,那么它是一个快速而肮脏的绑定方式。它将如下所示:

  public partial class MainWindow:Window 

{
public ObservableCollection< Directory> DirectoryList;

public MainWindow()
{
InitializeComponent();

DirectoryList = new ObservableCollection< Directory>();
目录_dirtemp = new Directory();
_dirtemp.IKey =1;
_dirtemp.IValue =史蒂夫;
DirectoryList.Add(_dirtemp);

_dirtemp = new Directory();
_dirtemp.IKey =2;
_dirtemp.IValue =John;
DirectoryList.Add(_dirtemp);


DataContext = DirectoryList;

}
}

窗口x:Class =DataBindCombo.MainWindow
xmlns =http://schemas.microsoft.com/winfx / 2006 / xaml / presentation
xmlns:x =http://schemas.microsoft.com/winfx/2006/xaml
xmlns:local =clr-namespace:DataBindCombo
Title =MainWindowHeight =350Width =525>
< Grid>
< ComboBox Height =48Horizo​​ntalAlignment =LeftMargin =70,104,0,0Name =comboBox1VerticalAlignment =TopWidth =310
ItemsSource ={Binding }
DisplayMemberPath =IValue
SelectedValuePath =IKey
>

另一种方式是更复杂,但可能会更频繁地使用。要做到这一点,您需要将您的MainWindow中的集合作为DependencyProperty进行公开,然后绑定到该值。它看起来像这样:

  public partial class MainWindow:Window 
{
public static DependencyProperty DirectoryListProperty =
DependencyProperty.Register(DirectoryList,
typeof(ObservableCollection< Directory>),
typeof(MainWindow));

public MainWindow()
{
InitializeComponent();
}

public ObservableCollection< Directory> DirectoryList
{
get {return(ObservableCollection< Directory>)base.GetValue(DirectoryListProperty); }
set {base.SetValue(DirectoryListProperty,value); }
}
}

< Window x:Class =MainWindow
xmlns =http://schemas.microsoft.com/winfx/2006/ xmll / presentation
xmlns:x =http://schemas.microsoft.com/winfx/2006/xaml
标题=MainWindowHeight =350宽度=525x: Name =mainWindow>
< Grid>
< ComboBox Height =48Horizo​​ntalAlignment =LeftMargin =70,104,0,0Name =comboBox1VerticalAlignment =TopWidth =310
ItemsSource ={Binding ElementName = mainWindow,Path = DirectoryList}
DisplayMemberPath =IValue
SelectedValuePath =IKey
/>

这也不是以这种方式完成它的唯一方法。一般而言,不是直接在控件上创建列表,您将创建一个视图模型。 是创建演示文稿的推荐方式,但我的示例给你一种在那里获得功能的方法。你可以玩和尝试不同的方法来做到这一点。我发现在WPF中总是有多种做事情的方式,而且它是找到最适合这种情况的事情。


I am new to WPF, C# and data binding. I am just trying to bind a simple combo box to an ObservedCollection. Here is the code:

 public class Directory
{
    private string _ikey;
    public string IKey
    {
        get
        {
            return _ikey;
        }
        set
        {
            _ikey = value;
        }
    }
    private string _ivalue;
    public string IValue
    {
        get
        {
            return _ivalue;
        }
        set
        {
            _ivalue = value;
        }
    }

}
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window

{
    public ObservableCollection<Directory> DirectoryList = new ObservableCollection<Directory>();

    public MainWindow()
    {
        InitializeComponent();

        DirectoryList = new ObservableCollection<Directory>();
        Directory _dirtemp = new Directory();
        _dirtemp.IKey = "1";
        _dirtemp.IValue = "Steve";
        DirectoryList.Add(_dirtemp);

        _dirtemp = new Directory();
        _dirtemp.IKey = "2";
        _dirtemp.IValue = "John";
        DirectoryList.Add(_dirtemp);




    }
}

My xaml looks like this:

<Window x:Class="DataBindCombo.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:DataBindCombo"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <ComboBox Height="48" HorizontalAlignment="Left" Margin="70,104,0,0" Name="comboBox1" VerticalAlignment="Top" Width="310"
              ItemsSource="{Binding Path=DirectoryList}"
              DisplayMemberPath="IValue"
              SelectedValuePath="IKey"
              >

It seems like it should be simple. I was able to put the binding in the code behind and it worked fine, but I would like to have it bind through xaml.

Any ideas? TIA

解决方案

There are a couple ways you can go about this. The basics are you need to make it so the XAML can see your collection. You can do this in an implicit manner by setting it to your DataContext. If this is the only thing you are binding then its a quick and dirty way of binding. It would look like this:

public partial class MainWindow : Window

{
    public ObservableCollection<Directory> DirectoryList;

    public MainWindow()
    {
        InitializeComponent();

        DirectoryList = new ObservableCollection<Directory>();
        Directory _dirtemp = new Directory();
        _dirtemp.IKey = "1";
        _dirtemp.IValue = "Steve";
        DirectoryList.Add(_dirtemp);

        _dirtemp = new Directory();
        _dirtemp.IKey = "2";
        _dirtemp.IValue = "John";
        DirectoryList.Add(_dirtemp);


        DataContext=DirectoryList;

    }
}

Window x:Class="DataBindCombo.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:DataBindCombo"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <ComboBox Height="48" HorizontalAlignment="Left" Margin="70,104,0,0" Name="comboBox1" VerticalAlignment="Top" Width="310"
              ItemsSource="{Binding}"
              DisplayMemberPath="IValue"
              SelectedValuePath="IKey"
              >

The other way is more sophisticated but probably what you will use more often. To do it You need to expose your collection in your MainWindow as a DependencyProperty and then bind to that value. It would look something like this:

public partial class MainWindow : Window
    {
        public static DependencyProperty DirectoryListProperty =
            DependencyProperty.Register("DirectoryList",
            typeof(ObservableCollection<Directory>),
            typeof(MainWindow));

        public MainWindow()
        {
            InitializeComponent();
        }

        public ObservableCollection<Directory> DirectoryList
        {
            get { return (ObservableCollection<Directory>)base.GetValue(DirectoryListProperty); }
            set { base.SetValue(DirectoryListProperty, value); }
        }
    }

<Window x:Class="MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525" x:Name="mainWindow">
    <Grid>
        <ComboBox Height="48" HorizontalAlignment="Left" Margin="70,104,0,0" Name="comboBox1" VerticalAlignment="Top" Width="310"
              ItemsSource=" {Binding ElementName=mainWindow, Path=DirectoryList}"
              DisplayMemberPath="IValue"
              SelectedValuePath="IKey"
              />

This is also not the only way to accomplish it in this manner. In general rather than creating the list directly on the control you would create a view model. The MVVM pattern is the recommend way of creating your presentation, but my examples give you a way of getting functionality out there. You can play and experiment with different ways of doing this. I've discovered there are always multiple ways of doing things in WPF and its a matter of finding the one that fits the situation the best.

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

07-28 00:06