WPF 组合框 - 预选择项目
我想从项目源中预先选择一个组合框(选择一个现有项目)。这是我的类模型和 xaml 语法。
Class ViewModelSample
{
Public List<Animal> Animals;
Public LivingBeing LivingBeingInst {get; set;}
}
Class LivingBeing
{
Public Animal AnimalInst {get; set;}
}
--------------------------------------------------------------------
<Combobox ItemsSource={Binding Animals} SelectedItem={Binding LivingBeingInst.AnimalInst}
DisplayMemeber = SomePropertyInAnimal>
--------------------------------------------------------------------------
但这是行不通的。我想要的是,当 xaml 显示时,我需要使用 SelectedItem 中给出的项目预先选择组合框。
非常感谢任何帮助。谢谢,玛尼
I want to pre-select a combobox, (select an existing item) from the itemssource. Here is my class model and xaml syntax.
Class ViewModelSample
{
Public List<Animal> Animals;
Public LivingBeing LivingBeingInst {get; set;}
}
Class LivingBeing
{
Public Animal AnimalInst {get; set;}
}
--------------------------------------------------------------------
<Combobox ItemsSource={Binding Animals} SelectedItem={Binding LivingBeingInst.AnimalInst}
DisplayMemeber = SomePropertyInAnimal>
--------------------------------------------------------------------------
But this doesnt work. All I want is, when the xaml shows up, I need the combobox to be pre-selected with the item given in SelectedItem.
Any help is much appreciated. Thanks, Mani
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
感谢您的意见,我已经解决了这个问题。我已经实现了 INotifyPropertyChanged 但这不是问题。
组合框的 SelectedItem 应该是 ItemsSource 中给定的 Collection 中的值之一。尽管 AnimalInst 属于 Animal 类型,但它不是 ItemsSource 中的对象之一。所以我所做的就是声明一个 SelectedAnimal 属性,并将其绑定到 SelectedItem。 “SelectedAnimal”将从“Animals”集合中返回与 AnimalInst.Name 进行比较的相同实例。如下所示的示例。 (时间表是集合)。
Thanks for the input and I have resolved this. I had implemented INotifyPropertyChanged but that wasnt the issue.
The combobox's SelectedItem should be one of the values from the Collection that is given in ItemsSource. Though, AnimalInst is of type Animal, it is not one of the objects in te ItemsSource. So what I did is, declared a SelectedAnimal property, binded that to SelectedItem. 'SelectedAnimal' will return the same instance from 'Animals' collection comparing AnimalInst.Name. An example as in below. (Schedules is the collection).
通常,您的 ViewModel 和 SelectedAnimal 属性中会有一个列表(在您的情况下是动物),在您的情况下,LivingBeingInst 属性似乎应该是从列表中选择的动物。
为了让类似的东西发挥作用,您可能需要编写一些代码来将 LivingBeing 转换为动物,然后通过 ValueConverter 返回。
但是,我相信您最好的选择是创建一个 SelectedAnimal 属性来存储选定的动物。
在构造函数中,或者在最初填充 Animals 列表的任何位置,都可以将 SelectedAnimal 设置为列表中的第一个元素。
Normally you'd have a List (in your case Animals) off of your ViewModel and a SelectedAnimal property, in your case, it appears that the LivingBeingInst property is supposed to be the selected animal from the list.
To get something like that to work, you'll probably have to write some code to convert a LivingBeing into an animal and back via ValueConverter.
However, I believe your best bet would be to create a SelectedAnimal property to store the selected Animal.
In your constructor, or wherever you initially populate the list of Animals, you can set the SelectedAnimal to the first element in the list.
SelectedItem
的初始值必须是作为 ComboBox 的ItemsSource
成员的对象的实例。在此示例中,LivingBeingInst.AnimalInst
需要位于Animals
集合中。您还可以拥有一个属性,用于在
Animals
集合中查找与LivingBeingInst.AnimalInst
相对应的项目,就像 Everything Matters 的答案所做的那样。The initial value of the
SelectedItem
needs to be an instance of an object that is a member of the ComboBox'sItemsSource
. In this example,LivingBeingInst.AnimalInst
would need to be in theAnimals
collection.You could also have a property that finds the corresponding item to
LivingBeingInst.AnimalInst
in theAnimals
collection, like how the Everything Matters 's answer does.您没有在这些类中实现更改通知。因此,除非您已在相应类的构造函数中填充了所有这些属性,否则绑定无法知道您已这样做。
至少,这是我根据您发布的无效、不可编译、非真正代码的猜测。一般来说,如果您发布真实的代码,您将获得更有用的答案,特别是如果您花时间实现现有代码的最小子集,该子集仍然表现出您要解决的问题。 (除此之外,当你这样做时,你可能会自己找到答案。)
You don't have change notification implemented in these classes. So unless you've populated all of these properties in the respective classes' constructors, there's no way for the bindings to know that you've done so.
At least, that's my guess from the non-working, non-compilable, not-really-code that you've posted. Generally speaking, you'll get more useful answers if you post real code, especially if you take the time to implement the smallest minimal subset of your existing code that still exhibits the problem you're trying to solve. (Among other things, you might find the answer yourself when you do that.)