在绑定到 CollectionViewSource 的 Combobox 中设置初始值
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
默认情况下,将检查对象是否通过引用而不是值相等。
因此,如果
_ticket.SelectedSubstatus
不直接引用SubstatusList
中的项目,则SelectedSubstatus
将为 NULL,因为您正在尝试设置>SelectedSubstatus
等于SubstatusList
中不存在的项目要解决此问题,请覆盖
.Equals()
方法Substatus
如果对象的数据相同则返回 true。例如,By default, objects are checked if they are equal by reference, not value.
So if
_ticket.SelectedSubstatus
does not directly reference an item inSubstatusList
, then theSelectedSubstatus
will be NULL because you are trying to set theSelectedSubstatus
equal to an item that doesn't exist in theSubstatusList
To get around this, overwrite the
.Equals()
method ofSubstatus
to return true if an object's data is the same. For example,