locked
Multiple DataContexts for a Single Element RRS feed

  • Question

  • Hello

     

    I have a checkbox inside a DatagridTemplatecolumn. The datagrid is bound to a collection.

    The checkbox, checked state comes from the collection(datacontext1) whereas its visibility comes from the datacontext/viewmodel (datacontext2).

    What is the best practice here?

     

     <CheckBox x:Name="chkSelectPrimary" VerticalAlignment="Center" IsChecked="{Binding IsPrimary, Mode=TwoWay}"
                                         Visibility="{Binding ColumnVisibility <?PageDataContext?>}" HorizontalAlignment="Center" HorizontalContentAlignment="Center"/>


    Creativity Flows
    Wednesday, January 18, 2012 8:35 PM

All replies

  • You can specify the source of your binding in XAML. By default, the Source property on a binding is inherited, but you can override it to be pretty much whatever you can access.

    http://msdn.microsoft.com/en-us/library/system.windows.data.binding.source.aspx

     

    As an example, yours might look like this:

    <CheckBox x:Name="chkSelectPrimary" VerticalAlignment="Center" IsChecked="{Binding Property1, Mode=TwoWay}"
    	Visibility="{Binding Property2, Source={StaticResource ViewModel2}}" HorizontalAlignment="Center" HorizontalContentAlignment="Center"/>
    

    Wednesday, January 18, 2012 8:53 PM