2011-04-26 140 views
1

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

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




    } 
} 

我的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="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" 
       > 

现在看来似乎应该是简单的。我能够将绑定放在代码后面,它工作正常,但我希望通过xaml绑定它。

任何想法? TIA

回答

3

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

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" 
       > 

另一种方式更复杂,但可能会更频繁地使用。要做到这一点你需要将您的集合在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/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" 
       /> 

这也不是以这种方式完成它的唯一方法。一般来说,不是直接在控件上创建列表,而是创建视图模型。 MVVM pattern是创建演示文稿的推荐方式,但我的示例为您提供了一种获取功能的方法。您可以玩这个游戏并尝试不同的方式。我发现在WPF中总是有多种方式来处理事情,并且找到最适合情况的方法。

1

绑定中的路径相对于本地DataContext而不是包含Window。您尚未为您的Window设置DataContext,因此WPF没有查找DirectoryList属性的对象。

使用您当前的对象模型,您需要在MainWindow构造函数中设置this.DataContext = this;。现在,MainWindow对象将是DataContext,DirectoryList绑定将根据MainWindow对象进行解析。 (从更长期来看,更好的做法是将数据模型移出到单独的类,并将DataContext设置为该类的一个实例,但这是一个单独的问题。)

此外,WPF只能绑定到属性,不是领域。您的DirectoryList属性当前是一个字段;您将需要将其更改为属性。

0

您需要设置窗口的DataContext。例如像这样:

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

点的结合,背后的代码:

ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type Window}},Path=DirectoryList}" 
1

您需要做的是将ComboBox的DataContext设置为等于您的ObservableCollection。

代码隐藏:

public partial class MainWindow : Window 
{ 
    private 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); 

     this.comboBox1.DataContext = DirectoryList; 
     //OR for the entire window you can simply do this.DataContext = DirectoryList; 
    } 

    public ObservableCollection<Directory> DirectoryList 
    { 
     get { return directoryList; } 
    } 
} 

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

} 

的XAML:

<Window x:Class="WpfApplication3.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="MainWindow" Height="84" Width="167"> 
<Grid> 
    <ComboBox Height="23" HorizontalAlignment="Left" Margin="12,12,0,0" Name="comboBox1" VerticalAlignment="Top" Width="120" ItemsSource="{Binding}"/> 
</Grid> 

虽然,正如其他人所指出的,一个更好的办法是使用模型 - 视图 - 视图模型设计模式。这将使View(xaml部分)与“业务逻辑”分开。这里是一个很好的例子,介绍如何使用MVVM绑定组合框。

http://mark-dot-net.blogspot.com/2009/03/binding-combo-boxes-in-wpf-with-mvvm.html