简体   繁体   中英

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.

So are there any workarounds for accessing Columns property in model class?

My XAML code is like this :

<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 . 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.

On a side note, " access data grid's columns collection in my model " sounds rather strange for MVVM. Normally model shouldn`t even know what`sa DataGrid .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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