Embedding a user control

Jan 19, 2011 at 6:58 PM

I am trying to figure out the best way to embed a user control in my current view ( which is not the root ).

Let's say we have an 'AddressView' (and AddressViewModel ).  On this view, I want to embed a reusable control , that I can also pass parameters to.

Our XAML might look like this: 

 <Grid x:Name="LayoutRoot" Margin="2">
		<!-- Address View -->
        ...
        <StackPanel Grid.Row="1">
            <TextBlock Text="States:"/>
            <ContentControl x:Name="StateDropDownUserControl" />
        </StackPanel>  
    </Grid>

Now, StateDropDownUserControl, is a property on my AddressViewModel class and it is of type StateSelectorViewModel. Now CM will load the view associate with

StateSelectorViewModel for me, but how do I pass parameters and data context to it?

( i.e. IncludeOnly48ContigousStates = true ). Is this the correct way to do this kind of embedding user controls ?

 

Thanks in advance!

Coordinator
Jan 19, 2011 at 7:16 PM

A DataContext of StateSelectorViewModel will be automatically set up for the StateSelectorView. What do you need to pass a parameter too? I would imagine that IncludeOnly48... would be a property on the StateSelectorVM and that setting it would cause the VM to populate its list appropriately.

Jan 19, 2011 at 7:31 PM
Edited Jan 19, 2011 at 7:40 PM
EisenbergEffect wrote:

A DataContext of StateSelectorViewModel will be automatically set up for the StateSelectorView. What do you need to pass a parameter too? I would imagine that IncludeOnly48... would be a property on the StateSelectorVM and that setting it would cause the VM to populate its list appropriately.

Yes, you are right.  However, how can I pass the IncludeOnly48ContigousStates defined in the AddressView 

 

 
<ContentControl x:Name="StateDropDownUserControl" IncludeOnly48ContigousStates="True" /

 

down to the child StateSelectorView ? I was trying to avoid defining this property in both of these viewmodels .

 

UPDATE: Nevermind, I guess I am thinking View-First here.  I should just set 

IncludeOnly48ContigousStates off of this property: AddressVM.StateSelectorVM , basically setting up the 
StateSelectorVM in the AddressVM.