我是 WPF、C# 和数据绑定(bind)的新手。我只是想将一个简单的组合框绑定(bind)到一个 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>
/// 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"
>
看起来应该很简单。我能够将绑定(bind)放在后面的代码中并且工作正常,但我希望通过 xaml 绑定(bind)它。
有什么想法吗?时间差
最佳答案
有几种方法可以解决这个问题。您需要做的基础知识才能使 XAML 可以看到您的集合。您可以通过将其设置为您的 DataContext 以隐式方式执行此操作。如果这是您唯一要绑定(bind)的东西,那么它是一种快速而肮脏的绑定(bind)方式。它看起来像这样:
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 公开,然后绑定(bind)到该值。它看起来像这样:
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"
/>
这也不是以这种方式完成它的唯一方法。通常,您不会直接在控件上创建列表,而是创建一个 View 模型。 MVVM pattern是创建演示文稿的推荐方式,但我的示例为您提供了一种获得功能的方式。您可以尝试不同的方法来做到这一点。我发现在 WPF 中总是有多种做事的方法,关键是要找到最适合这种情况的方法。
关于c# - WPF 数据绑定(bind)到组合框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5784715/