MVVM WPF datagrid选择绑定到具有复杂对象的组合框
我试图实现一个DataGrid,其中一列有一个复杂的对象,网格外的comboBox有一个该列的同一时间对象的列表。使用WPF和MVVM 在datagrid中选择行时,我希望组合框显示相应的值 但我不知道我做错了什么。 有人能帮忙吗MVVM WPF datagrid选择绑定到具有复杂对象的组合框,wpf,vb.net,mvvm,combobox,datagrid,Wpf,Vb.net,Mvvm,Combobox,Datagrid,我试图实现一个DataGrid,其中一列有一个复杂的对象,网格外的comboBox有一个该列的同一时间对象的列表。使用WPF和MVVM 在datagrid中选择行时,我希望组合框显示相应的值 但我不知道我做错了什么。 有人能帮忙吗 <ComboBox x:Name="cmbResourceList" HorizontalAlignment="Left" ItemsSource="{Binding MainListSourc
<ComboBox x:Name="cmbResourceList"
HorizontalAlignment="Left"
ItemsSource="{Binding MainListSource}"
DisplayMemberPath="Description"
SelectedValue="{Binding Path=ScheduleVM.Schedule.ResourceList}"
Width="185"/>
<DataGrid x:Name="dgScheduleItems"
AutoGenerateColumns="False"
ItemsSource="{Binding Path=ScheduleSource}"
SelectionMode="Single"
IsReadOnly="True"
SelectedItem="SelectedSchedule">
<DataGrid.Columns>
<DataGridTextColumn Header="Id" Binding="{Binding Id}" Width="40"/>
<DataGridTextColumn Header="Resource List" Binding="{Binding ResourceList.Description}" Width="*"/>
<DataGridTextColumn Header="Task List" Binding="{Binding TaskList.Description}" Width="*"/>
<DataGridTextColumn Header="Description" Binding="{Binding Description}" Width="100"/>
</DataGrid.Columns>
</DataGrid>
有几件事
组合框上的两个SelectedValue都绑定到项目集合。如果不希望这样做,这些属性将绑定到项目集合中的各个项目
接下来,您对DataGrid的SelectedItem的绑定是错误的。将其更新为通知绑定关键字和大括号:
<DataGrid x:Name="dgScheduleItems"
AutoGenerateColumns="False"
ItemsSource="{Binding Path=ScheduleSource}"
SelectionMode="Single"
IsReadOnly="True"
SelectedItem="{Binding SelectedSchedule}"
Margin="0,106,0,0">
然后,在SelectedSchedule属性更改时设置新属性MainListSourceSelectedValue。类似于c
public MainList MainListSourceSelectedValue
{
get { return _mainListSourceSelectedValue; }
set
{
if (_mainListSourceSelectedValue == value) return;
_mainListSourceSelectedValue = value;
RaisePropertyChanged();
}
}
public Schedule SelectedSchedule
{
get { return _selectedSchedule; }
set
{
if (_selectedSchedule == value) return;
_selectedSchedule = value;
foreach (var mainListItem in MainList)
{
if (mainListItem.Description.equals(_selectedSchedule.ResourseList.Description))
{
MainListSourceSelectedValue = mainListItem;
break;
}
}
RaisePropertyChanged();
}
}
它起作用了,但我必须修改一下你的代码。。。我不能将组合绑定到SelectedSchedule的属性对象吗?我总是要在我的VM中声明一个要绑定的新属性吗?我建议对您的答案进行一些更改,所以这应该可以工作了。非常感谢您的帮助@d。moncada@andrepaulo,是的,我接受了你的编辑。我犯了愚蠢的错误,写得太快了!我指出这一点是因为如果其他人也有同样的问题而且知识较少。。。所以为什么被否决?至少留下一条注释所提供的大部分代码似乎不需要重现您的问题,也不够。这有两个主要的缺点1读者必须做的工作是过滤什么是相关的,什么不是2它使你的问题不可能对任何其他人有帮助,而不仅仅是你,因为它不是通用的。当您似乎无法通过猜测来纠正错误时,第一步是通过增量删除代码来隔离错误,直到您无法在不删除错误的情况下删除任何内容为止。然后,如果您仍然看不出哪里出了问题,请发布生成的代码和您的问题。@franssu我尽了最大努力使其尽可能简洁,谢谢您的评论。我只是想澄清一下,这里的人们应该尊重提问者目前的局限性,我们知道什么时候有人在问是否懒惰。顺便说一句,我已经更改了问题的代码以符合您的要求。@franssu再次更新了代码,符合需要吗?它正朝着正确的方向发展。不过,这并不是关于这个问题,而是关于你在职业生涯中会遇到的所有错误,以及你将来会问的所有问题。要找到一个bug,最好的方法就是尽可能地隔离它。
<ComboBox x:Name="cmbTaskList"
HorizontalAlignment="Left"
ItemsSource="{Binding MainListSource}"
DisplayMemberPath="Description"
SelectedValue="{Binding MainListSourceSelectedValue, Mode=TwoWay}"
Margin="244,30,0,0"
VerticalAlignment="Top"
Width="185"/>
public MainList MainListSourceSelectedValue
{
get { return _mainListSourceSelectedValue; }
set
{
if (_mainListSourceSelectedValue == value) return;
_mainListSourceSelectedValue = value;
RaisePropertyChanged();
}
}
public Schedule SelectedSchedule
{
get { return _selectedSchedule; }
set
{
if (_selectedSchedule == value) return;
_selectedSchedule = value;
foreach (var mainListItem in MainList)
{
if (mainListItem.Description.equals(_selectedSchedule.ResourseList.Description))
{
MainListSourceSelectedValue = mainListItem;
break;
}
}
RaisePropertyChanged();
}
}