.net WPF 将集合绑定到 ComboBox 并选择一个项目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/310805/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF Binding Collection To ComboBox and Selecting an item
提问by Adam Driscoll
I've been knocking my head against this for some time now. I'm not really sure why it isn't working. I'm still pretty new to this whole WPF business.
一段时间以来,我一直在反对这一点。我不确定为什么它不起作用。我对整个 WPF 业务还是很陌生。
Here's my XAML for the combobox
这是我的组合框 XAML
<ComboBox
SelectedValuePath="Type.FullName"
SelectedItem="{Binding Path=Type}"
Name="cmoBox" />
Here's what populates the ComboBox (myAssembly is a class I created with a list of possible types)
这是填充 ComboBox 的内容(myAssembly 是我使用可能类型列表创建的类)
cmoBox.ItemsSource = myAssembly.PossibleTypes;
I set the DataContext in a parent element of the ComboBox in the code behind like this:
我在后面的代码中将 DataContext 设置在 ComboBox 的父元素中,如下所示:
groupBox.DataContext = listBox.SelectedItem;
I want the binding to select the correct "possible type" from the combo box. It doesn't select anything. I have tried SelectedValue and SelectedItem. When I changed the DisplayMemberPath of the ComboBox to a different property it changed what was displayed so I know it's not completely broken.
我希望绑定从组合框中选择正确的“可能类型”。它不选择任何东西。我试过 SelectedValue 和 SelectedItem。当我将 ComboBox 的 DisplayMemberPath 更改为不同的属性时,它更改了显示的内容,因此我知道它没有完全损坏。
Any ideas???
有任何想法吗???
回答by TabbyCool
You could also set the binding in the xaml rather than in the code-behind (we avoid code behind in our xaml pages wherever possible). I'm assuming that myAssembly is a property on your code-behind for the control and is an instance of your MyAssembly class...
您还可以在 xaml 中而不是在代码隐藏中设置绑定(我们尽可能避免在我们的 xaml 页面中隐藏代码)。我假设 myAssembly 是您的代码隐藏控件的一个属性,并且是您的 MyAssembly 类的一个实例......
<UserControl
x:Class="MyNamespace.MyControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
DataContext="{Binding}">
<ComboBox
Width="200"
ItemsSource="{Binding Path=myAssembly.PossibleTypes}"
SelectedValuePath="Type.FullName"
SelectedItem="{Binding Path=Type}"
Name="cmoBox" />
</UserControl>
It may just be personal preference, but I think it's better practice to have the data bindings in the xaml since it makes it easier to see what each control is bound to without having to rake through the code-behind. Also, if you want to refer to your ComboBox from within code, you should assign an x:Name property to it in the xaml rather than just Name.
这可能只是个人偏好,但我认为在 xaml 中包含数据绑定是更好的做法,因为这样可以更轻松地查看每个控件绑定到的内容,而无需深入研究代码隐藏。此外,如果您想从代码中引用您的 ComboBox,您应该在 xaml 中为其分配一个 x:Name 属性,而不仅仅是名称。
回答by Timothy Khouri
In the XAML, set ItemsSource="{Binding}"and (in the code behind) set the DataContextto myAssembly.PossibleTypes.
在 XAML 中,设置ItemsSource="{Binding}"和(在后面的代码中)设置DataContext为myAssembly.PossibleTypes.
回答by Rick O'Shea
I agree: bindings should be in the XAML. I put ... checking .. nothing at all in the code behind, ever. Data sources are all re-usable "resources".
我同意:绑定应该在 XAML 中。我把......检查......在后面的代码中什么都没有,永远。数据源都是可重复使用的“资源”。
(well, OK, the code-behind constructor calls InitializeComponent()).
(好吧,代码隐藏构造函数调用 InitializeComponent())。

