I created a view and viewmodel that I would like to use twice (or more) in the same application. I would like however to have it bound to two different models.
<TabItem Background="Transparent">
<TabItem.Header>
<TextBlock Text="Items" Foreground="LightSeaGreen" FontSize="14"/>
</TabItem.Header>
<AdornerDecorator>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="250" />
</Grid.ColumnDefinitions>
<local:ItemsView Grid.Row="0" Grid.Column="0" Margin="5" Height="Auto"/>
//This is the reused control
<local:NutritionLabelView Grid.Row="0" Grid.Column="1" Margin="5,15,5,5" Height="Auto" VerticalAlignment="Top" ViewKey="Key_ItemsView" />
</Grid>
</AdornerDecorator>
</TabItem>
<TabItem Background="Transparent">
<TabItem.Header>
<TextBlock Text="Meals" Foreground="LightSeaGreen" FontSize="14"/>
</TabItem.Header>
<AdornerDecorator>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="250" />
</Grid.ColumnDefinitions>
<local:MealsView Grid.Column="0" Margin="5" Height="Auto"/>
//This is it again
<local:NutritionLabelView Grid.Column="1" Margin="5,15,5,5" Height="Auto" VerticalAlignment="Top" ViewKey="Key_MealsView" />
</Grid>
</AdornerDecorator>
</TabItem>
The control that is reused is the <local:NutritionLabelView Grid.Column="1" Margin="5,15,5,5" Height="Auto" VerticalAlignment="Top" ViewKey="Key_MealsView" /> which I gave a ViewKey property to so I could tell which data this view should display. Can I use a multibinding for this?
I figured this out by binding each of the view’s controls to the same property in the viewmodel and made the property of type Object. Then based on the view key returned the correct type of object. Works for me.