在绑定到 CollectionViewSource 的 Combobox 中设置初始值

发布于 2024-11-28 22:04:06 字数 1808 浏览 1 评论 0原文

我正在使用 WPF 和 MVVM 模式。所有绑定到列表的组合框都可以正常工作,但我有一个级联下拉列表,它使用 CollectionViewSource 进行过滤。过滤有效,设置器也有效(在寻找答案时,我看到另一个人遇到了问题),但我无法设置初始值。我尝试了几种方法,但似乎都没有影响所选项目。

Viewmodel ctor 和 Property setter(_ticket.SelectedSubstatus 在模型构造函数中设置):

public TicketViewModel()
    {
        _ticket = new TicketModel();
        SubstatusList = CollectionViewSource.GetDefaultView(GetStatusList());
        SubstatusList.Filter = (x) => { return (int)(x as Substatus).IST_MAIN_STATUS == (int)SelectedStatus.IST_STATUS_ID; };

        SubstatusList.MoveCurrentTo(_ticket.SelectedSubstatus);
        SelectedSubstatus = _ticket.SelectedSubstatus;

        Substatus test = (Substatus)SubstatusList.CurrentItem;

    }

public Substatus SelectedSubstatus
    {
        get { return _ticket.SelectedSubstatus; }
        set
        {
            if (value == _ticket.SelectedSubstatus ||value == null)
                return;

            _ticket.SelectedSubstatus = value;
            _ticket.Issue.IS_SUBSTATUS_ID = value.IST_SUBSTATUS_ID;

            base.OnPropertyChanged("SelectedSubstatus");
        }
    }

这里是组合框 XAML

<ComboBox HorizontalAlignment="Stretch" Margin="15,0,0,0"
                                  Name="comboBox1" VerticalAlignment="Bottom" 
                                  Grid.Column="2" Grid.Row="1" FontSize="12" 
                                  IsSynchronizedWithCurrentItem="True"
                                  ItemsSource="{Binding Path=SubstatusList}" 
                                  SelectedItem="{Binding Path=SelectedSubstatus, Mode=TwoWay}" 
                                  DisplayMemberPath="IST_NAME"/>

在通过 MoveCurrentTo() 设置后,CollectionViewSource 中的当前项为 null,并且当通过测试检查。我做错了什么?

I am using WPF and an MVVM pattern. All the comboboxes that are bound to lists work fine, but I have a cascading dropdown that uses a CollectionViewSource for filtering. The filtering works, as does the setter (which while searching for an answer, I saw another person having trouble with), but I am unable to set the initial value. I have tried a few methods, but none seem to affect the selecteditem.

Viewmodel ctor, and Property setter (_ticket.SelectedSubstatus is set in the model constructor):

public TicketViewModel()
    {
        _ticket = new TicketModel();
        SubstatusList = CollectionViewSource.GetDefaultView(GetStatusList());
        SubstatusList.Filter = (x) => { return (int)(x as Substatus).IST_MAIN_STATUS == (int)SelectedStatus.IST_STATUS_ID; };

        SubstatusList.MoveCurrentTo(_ticket.SelectedSubstatus);
        SelectedSubstatus = _ticket.SelectedSubstatus;

        Substatus test = (Substatus)SubstatusList.CurrentItem;

    }

public Substatus SelectedSubstatus
    {
        get { return _ticket.SelectedSubstatus; }
        set
        {
            if (value == _ticket.SelectedSubstatus ||value == null)
                return;

            _ticket.SelectedSubstatus = value;
            _ticket.Issue.IS_SUBSTATUS_ID = value.IST_SUBSTATUS_ID;

            base.OnPropertyChanged("SelectedSubstatus");
        }
    }

and here is the combobox XAML

<ComboBox HorizontalAlignment="Stretch" Margin="15,0,0,0"
                                  Name="comboBox1" VerticalAlignment="Bottom" 
                                  Grid.Column="2" Grid.Row="1" FontSize="12" 
                                  IsSynchronizedWithCurrentItem="True"
                                  ItemsSource="{Binding Path=SubstatusList}" 
                                  SelectedItem="{Binding Path=SelectedSubstatus, Mode=TwoWay}" 
                                  DisplayMemberPath="IST_NAME"/>

The current item from the CollectionViewSource is null just after being set by MoveCurrentTo(), and when checked by test. What am I doing wrong?

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

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

发布评论

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

评论(1

停滞 2024-12-05 22:04:06

默认情况下,将检查对象是否通过引用而不是值相等。

因此,如果 _ticket.SelectedSubstatus 不直接引用 SubstatusList 中的项目,则 SelectedSubstatus 将为 NULL,因为您正在尝试设置 >SelectedSubstatus 等于 SubstatusList 中不存在的项目

要解决此问题,请覆盖 .Equals() 方法Substatus 如果对象的数据相同则返回 true。例如,

public override bool Equals(object obj)
{
      if (obj == null) return false;
      if (obj.GetType() != this.GetType()) return false;

      return this.Id == ((SubStatus)obj).Id;
 }

By default, objects are checked if they are equal by reference, not value.

So if _ticket.SelectedSubstatus does not directly reference an item in SubstatusList, then the SelectedSubstatus will be NULL because you are trying to set the SelectedSubstatus equal to an item that doesn't exist in the SubstatusList

To get around this, overwrite the .Equals() method of Substatus to return true if an object's data is the same. For example,

public override bool Equals(object obj)
{
      if (obj == null) return false;
      if (obj.GetType() != this.GetType()) return false;

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