从视图模型中获取列表中的选定项目
早上好!
我正在编写一个管理应用程序,它使用 WPF 和 MVVM 模式显示任务列表。用户也可以编辑此列表,但我想使用删除按钮来删除所选行。问题是我不知道如何从 ModelView 访问所选行,并且我不想使用代码隐藏。我想最好的选择是使用一些绑定,但我不确定如何做到这一点。
这是代码(我正在使用 DevXpress 控件,但几乎相同)
<dxg:GridControl ItemsSource="{Binding Path=TaskView}" Loaded="GridControl_Loaded">
<dxg:GridControl.Columns>
<dxg:GridColumn Visible="False" DisplayMemberBinding="{Binding Path=ID, Mode=OneWay}" />
<dxg:GridColumn Header="Project" DisplayMemberBinding="{Binding Path=ProjectCode, Mode=TwoWay}" AllowEditing="True" ContentChanged="OnHoursChanged"/>
<dxg:GridColumn Header="Project Description" DisplayMemberBinding="{Binding Path=ProjectName, Mode=TwoWay}" AllowEditing="False"/>
<dxg:GridColumn Header="Description" DisplayMemberBinding="{Binding Path=ProjectDescription, Mode=TwoWay}" AllowEditing="True"/>
<dxg:GridColumn Header="Comment" DisplayMemberBinding="{Binding Path=Comment, Mode=TwoWay}" AllowEditing="True"/>
<dxg:GridColumn Header="{Binding ElementName=mon,Path=Text}" DisplayMemberBinding="{Binding Path=HoursMonday, Mode=TwoWay}" AllowEditing="True" FieldName="M" />
<dxg:GridColumn Header="{Binding ElementName=tue,Path=Text}" DisplayMemberBinding="{Binding Path=HoursTuesday, Mode=TwoWay}" AllowEditing="True" FieldName="T" />
<dxg:GridColumn Header="{Binding ElementName=wed,Path=Text}" DisplayMemberBinding="{Binding Path=HoursWednesday, Mode=TwoWay}" AllowEditing="True" FieldName="W" />
<dxg:GridColumn Header="{Binding ElementName=thu,Path=Text}" DisplayMemberBinding="{Binding Path=HoursThursday, Mode=TwoWay}" AllowEditing="True" FieldName="Th" />
<dxg:GridColumn Header="{Binding ElementName=fri,Path=Text}" DisplayMemberBinding="{Binding Path=HoursFriday, Mode=TwoWay}" AllowEditing="True" FieldName="F" />
<dxg:GridColumn Header="{Binding ElementName=sat,Path=Text}" DisplayMemberBinding="{Binding Path=HoursSaturday, Mode=TwoWay}" AllowEditing="True" FieldName="Sat" />
<dxg:GridColumn Header="{Binding ElementName=sun,Path=Text}" DisplayMemberBinding="{Binding Path=HoursSunday, Mode=TwoWay}" AllowEditing="True" FieldName="Sun" />
<dxg:GridColumn Header="Total" DisplayMemberBinding="{Binding Path=Total, Mode=OneWay}" AllowEditing="False" FieldName="Tot" />
</dxg:GridControl.Columns>
</dxg:GridControl>
您有什么想法吗?
非常感谢!
Good morning!
I'm programming a managment aplication that shows a task list using WPF and MVVM patern. Users can edit this list too but I want to use a delete button to delete selected row. The problem is that I don't know how to access to the selected row from ModelView and I don't want to use code-behind. I guess the best option is using some binding but I'm unsure about how to do it.
This is the code ( I'm using DevXpress controls but is almost the same )
<dxg:GridControl ItemsSource="{Binding Path=TaskView}" Loaded="GridControl_Loaded">
<dxg:GridControl.Columns>
<dxg:GridColumn Visible="False" DisplayMemberBinding="{Binding Path=ID, Mode=OneWay}" />
<dxg:GridColumn Header="Project" DisplayMemberBinding="{Binding Path=ProjectCode, Mode=TwoWay}" AllowEditing="True" ContentChanged="OnHoursChanged"/>
<dxg:GridColumn Header="Project Description" DisplayMemberBinding="{Binding Path=ProjectName, Mode=TwoWay}" AllowEditing="False"/>
<dxg:GridColumn Header="Description" DisplayMemberBinding="{Binding Path=ProjectDescription, Mode=TwoWay}" AllowEditing="True"/>
<dxg:GridColumn Header="Comment" DisplayMemberBinding="{Binding Path=Comment, Mode=TwoWay}" AllowEditing="True"/>
<dxg:GridColumn Header="{Binding ElementName=mon,Path=Text}" DisplayMemberBinding="{Binding Path=HoursMonday, Mode=TwoWay}" AllowEditing="True" FieldName="M" />
<dxg:GridColumn Header="{Binding ElementName=tue,Path=Text}" DisplayMemberBinding="{Binding Path=HoursTuesday, Mode=TwoWay}" AllowEditing="True" FieldName="T" />
<dxg:GridColumn Header="{Binding ElementName=wed,Path=Text}" DisplayMemberBinding="{Binding Path=HoursWednesday, Mode=TwoWay}" AllowEditing="True" FieldName="W" />
<dxg:GridColumn Header="{Binding ElementName=thu,Path=Text}" DisplayMemberBinding="{Binding Path=HoursThursday, Mode=TwoWay}" AllowEditing="True" FieldName="Th" />
<dxg:GridColumn Header="{Binding ElementName=fri,Path=Text}" DisplayMemberBinding="{Binding Path=HoursFriday, Mode=TwoWay}" AllowEditing="True" FieldName="F" />
<dxg:GridColumn Header="{Binding ElementName=sat,Path=Text}" DisplayMemberBinding="{Binding Path=HoursSaturday, Mode=TwoWay}" AllowEditing="True" FieldName="Sat" />
<dxg:GridColumn Header="{Binding ElementName=sun,Path=Text}" DisplayMemberBinding="{Binding Path=HoursSunday, Mode=TwoWay}" AllowEditing="True" FieldName="Sun" />
<dxg:GridColumn Header="Total" DisplayMemberBinding="{Binding Path=Total, Mode=OneWay}" AllowEditing="False" FieldName="Tot" />
</dxg:GridControl.Columns>
</dxg:GridControl>
Do you have any ideas?
Thank you very much!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的虚拟机应该有一个 Project SelectedProject { get;放; } 属性。
将 SelectedRow (或其他内容)从 Grid 移到它的 bin 中:
Your VM should have a
Project SelectedProject { get; set; }
property.The bin the SelectedRow (or something) from the Grid to it:
像这样的事情:
Something like this: