我是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