简体   繁体   English

MVVM Silverlight DataGrid:绑定列集合

[英]MVVM Silverlight DataGrid : Binding Columns Collection

I need to access data grid's columns collection in my model class but when i try this it says that the data grid's columns property doesn't has an accessible setter. 我需要在模型类中访问数据网格的columns集合,但是当我尝试此操作时,它说数据网格的columns属性没有可访问的setter。

So are there any workarounds for accessing Columns property in model class? 那么,是否有任何变通办法来访问模型类中的Columns属性?

My XAML code is like this : 我的XAML代码是这样的:

<sdk:DataGrid RowDetailsVisibilityMode="VisibleWhenSelected"  Columns="{Binding GridColumns, Mode=TwoWay}"
                      AutoGenerateColumns="False" IsReadOnly="True" SelectionMode="Single" 
                      ClipboardCopyMode="ExcludeHeader" HorizontalAlignment="Stretch" 
                      VerticalAlignment="Stretch" Margin="0,0,0,27" HeadersVisibility="Column" Grid.Row="1"
                      SelectedItem="{Binding SelectedItem, Mode=TwoWay}"
                      ItemsSource="{Binding DataList, Mode=TwoWay, ValidatesOnDataErrors=True, NotifyOnValidationError=True}" >

You can`t bind to Columns property because it`s not DependencyProperty . 您不能绑定到Columns属性,因为它不是DependencyProperty So just add columns from code if you need to pass them from somewhere else. 因此,如果您需要从其他地方传递它们,只需添加代码中的列即可。 If they are not context dependent then assigning them in XAML would probably be better. 如果它们不是上下文相关的,则在XAML中分配它们可能会更好。

On a side note, " access data grid's columns collection in my model " sounds rather strange for MVVM. 附带一提,“ 访问我模型中的数据网格的列集合 ”对于MVVM听起来很奇怪。 Normally model shouldn`t even know what`sa DataGrid . 通常,模型甚至都不知道DataGrid是什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM