WPF 组合框 - 预选择项目

发布于 2024-10-19 10:21:55 字数 622 浏览 1 评论 0原文

我想从项目源中预先选择一个组合框(选择一个现有项目)。这是我的类模型和 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

秋日私语 2024-10-26 10:21:55

感谢您的意见,我已经解决了这个问题。我已经实现了 INotifyPropertyChanged 但这不是问题。

组合框的 SelectedItem 应该是 ItemsSource 中给定的 Collection 中的值之一。尽管 AnimalInst 属于 Animal 类型,但它不是 ItemsSource 中的对象之一。所以我所做的就是声明一个 SelectedAnimal 属性,并将其绑定到 SelectedItem。 “SelectedAnimal”将从“Animals”集合中返回与 AnimalInst.Name 进行比较的相同实例。如下所示的示例。 (时间表是集合)。

 private ISchedule _selectedSchedule;
       public ISchedule SelectedSchedule
        {
            get
            {
                if(_selectedSchedule != null)
                {
                    var schedule = this.Schedules.Where(item => item.GlobalCodesId == _selectedSchedule.GlobalCodesId).FirstOrDefault();
                    return schedule;
                }
                return _selectedSchedule;
            }
           set 
           { 
               _selectedSchedule = value;
               base.NotifyPropertyChanged("SelectedSchedule");
           }
        }

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

 private ISchedule _selectedSchedule;
       public ISchedule SelectedSchedule
        {
            get
            {
                if(_selectedSchedule != null)
                {
                    var schedule = this.Schedules.Where(item => item.GlobalCodesId == _selectedSchedule.GlobalCodesId).FirstOrDefault();
                    return schedule;
                }
                return _selectedSchedule;
            }
           set 
           { 
               _selectedSchedule = value;
               base.NotifyPropertyChanged("SelectedSchedule");
           }
        }
可遇━不可求 2024-10-26 10:21:55

通常,您的 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.

迷迭香的记忆 2024-10-26 10:21:55

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's ItemsSource. In this example, LivingBeingInst.AnimalInst would need to be in the Animals collection.

You could also have a property that finds the corresponding item to LivingBeingInst.AnimalInst in the Animals collection, like how the Everything Matters 's answer does.

因为看清所以看轻 2024-10-26 10:21:55

您没有在这些类中实现更改通知。因此,除非您已在相应类的构造函数中填充了所有这些属性,否则绑定无法知道您已这样做。

至少,这是我根据您发布的无效、不可编译、非真正代码的猜测。一般来说,如果您发布真实的代码,您将获得更有用的答案,特别是如果您花时间实现现有代码的最小子集,该子集仍然表现出您要解决的问题。 (除此之外,当你这样做时,你可能会自己找到答案。)

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文