WPF:ComboBox,根据识别属性比较对象

发布于 2024-10-08 19:04:02 字数 780 浏览 1 评论 0原文

假设我有一个类,我们将其命名为 Parent。这个类有另一个类的对象作为属性,我们称之为 Child。孩子有一个 int 属性 ID。

现在这些类的实例基于数据库表中的行。

因此,假设父级有 ID=4 的子级实例,在我的程序中将有一个包含所有可用子级实例的下拉列表,以便我们可以更改父级中的实例。

问题是,由于糟糕的设计,包含所有子对象的列表与父对象中的子对象在不同的​​情况下实例化,因此即使它们都有 ID = 4,它也不会将它们识别为同一个对象(因为当然它不是't)。

但是我仍然不希望相同的 ID 对象成为 ComboBox 的默认对象。我当然应该以某种方式引用 ID,但在 12 月的黑暗月份里我有点慢,而且我不知道如何做到这一点,因为它仍然是我设置的对象,而不仅仅是 int 值。

这是 XAML 代码:

 <DataTemplate x:Key="EditTemplate" DataType="{x:Type data:Parent}">
      <ComboBox ItemsSource="{Binding ElementName=Panel, Path=DataContext.ChildList}"
                              SelectedItem="{Binding Path=Child, Mode=TwoWay}"
                              SelectedValuePath="ID" DisplayMemberPath="Name" />
 </DataTemplate>

Let's say I have a Class, let's name it Parent. This class has as a property a object of another class, let´s call it Child. Child has a int property ID.

Now instances of these classes are based on rows in database tables.

So let's say Parent has Child instance with ID=4 and in my program there will be a Dropdown list with all available Child instances so we can change the instance in Parent.

Problem is that because of bad design the list with all the child objects is instanced on a seperate occasion from the Child inside the parent so even if they both have ID=4 it won't recognize them as same object ( because of course it isn't ).

However I still wan't the same ID object being the default one for the ComboBox. I should somehow of course just reference the ID somehow but I'm a little bit slow in the dark month of december and I'm not seeing how to do it because it's still the object I'm setting not only int value.

Here is the XAML code:

 <DataTemplate x:Key="EditTemplate" DataType="{x:Type data:Parent}">
      <ComboBox ItemsSource="{Binding ElementName=Panel, Path=DataContext.ChildList}"
                              SelectedItem="{Binding Path=Child, Mode=TwoWay}"
                              SelectedValuePath="ID" DisplayMemberPath="Name" />
 </DataTemplate>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

遗忘曾经 2024-10-15 19:04:02

所以即使它们都有 ID=4,它也不会将它们识别为同一个对象(因为当然不是)

听起来根本问题是相等,重写给定子对象的必要方法以达到定义适合您需求的平等。

public override bool Equals(object obj)
   {
      Child other = obj as Child;
      if( other == null )
      {
         return false;
      }
 
      return (this.Id == other.Id);
   }
 
   public override int GetHashCode()
   {
      return this.Id.GetHashCode();
   }
 
   public static bool operator == (Child me, Child other)
   {
      return Equals(me, other);
   }
 
   public static bool operator != (Child me, Child other)
   {
      return !Equals(me, other);
   }

so even if they both have ID=4 it won't recognize them as same object ( because of course it isn't )

It sounds like the root problem is equality, override the necessary methods for the given child object to reach the definition of equality that suits your needs.

public override bool Equals(object obj)
   {
      Child other = obj as Child;
      if( other == null )
      {
         return false;
      }
 
      return (this.Id == other.Id);
   }
 
   public override int GetHashCode()
   {
      return this.Id.GetHashCode();
   }
 
   public static bool operator == (Child me, Child other)
   {
      return Equals(me, other);
   }
 
   public static bool operator != (Child me, Child other)
   {
      return !Equals(me, other);
   }
西瓜 2024-10-15 19:04:02

如果其他人再次遇到此问题并需要 XAML 解决方案,您还可以使用 SelectedValuePath 和 SelectedValue 属性以及 SelectedItem 属性。

<DataTemplate x:Key="EditTemplate" DataType="{x:Type data:Parent}">
  <ComboBox ItemsSource="{Binding ElementName=Panel, Path=DataContext.ChildList}"
                          SelectedItem="{Binding Path=Child, Mode=TwoWay}"
                          SelectedValue="{Binding Path=Child.ID, Mode=OneWay}"
                          SelectedValuePath="ID" DisplayMemberPath="Name" />
</DataTemplate>

注意:显然,这不是对选择进行两个绑定的最佳实践,因此请谨慎使用。

In case anyone else comes across this again and wants a XAML solution, you can also use the SelectedValuePath and SelectedValue property along with the SelectedItem property.

<DataTemplate x:Key="EditTemplate" DataType="{x:Type data:Parent}">
  <ComboBox ItemsSource="{Binding ElementName=Panel, Path=DataContext.ChildList}"
                          SelectedItem="{Binding Path=Child, Mode=TwoWay}"
                          SelectedValue="{Binding Path=Child.ID, Mode=OneWay}"
                          SelectedValuePath="ID" DisplayMemberPath="Name" />
</DataTemplate>

NOTE: Obviously this isn't best practice to have two bindings to the selection, so use with caution.

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