我正在使用M-V-VM模式。
我有一个ViewModel和一个DataModels的ObservableCollection。
DataModels列表是数据绑定(bind)到DataGrid的。
渲染网格时,我希望其中一个字段是ComboBox(假设是字符串名称列表)。
此字符串名称列表是适用于所有行(即DataModels)的通用列表。
有没有办法将网格的字段级行属性绑定(bind)到父ViewModel?
我想避免的一种可能的解决方案是:
在DataModel中有一个get-property,它实际上返回ViewModel的属性(字符串名称列表)。
最佳答案
您可以使用静态资源来完成。
例如。在xaml中定义您的静态资源
<UserControl.Resources>
<mynamespace:MyViewModel x:Key="MyViewModel" />
</UserControl.Resources>
现在,您可以在用户控件中引用此资源:
<Controls::DataGrid DataContext="{StaticResource MyViewModel}" ItemSource="{Binding MyItems}" ...
<Controls:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding MyItems, Source={StaticResource MyViewModel}" DisplayMemberPath="MyString" /> <!-- This does the job with the combo box and the strings -->
</DataTemplate>
</Controls:DataGridTemplateColumn.CellTemplate>
</Controls:DataGrid>
希望这可以帮助,
BR
TJ
关于silverlight - 从数据模型绑定(bind)到其父ViewModel的属性?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4952492/