Search code examples
c#wpfmvvmdatatemplatecontentcontrol

ContentTemplate and Multiple View Dependency Properties


I have a window that displays a custom user control, a content control, and a textbox... When the user picks a radio button on the custom user control, a dependency property changes which in turn changes the view to be from a data template

The problem I'm having: I cannot properly retrieve the underlying dependency properties that are exposed on the swapped user control. For instance, each user control exposes an IsSearching dependency property.

Based on the value, I want to disable some functionality until the IsSearching is complete. I've tried setting the textbox text a couple of ways but can't find it the proper way to retrieve the binding.

I also tried binding the dependency property to a property on the mainviewmodel (CTALightViewModel) but it doesn't seem to be working correctly. Definitely a little lost on this so any help is appreciated.

<views:CTAAddress x:Name="CTAAddressView" IsSearching="{Binding VMBusy, Mode=OneWay}"/>

DataTemplates

<Window.Resources>
    <DataTemplate x:Key="AddressTemplate" DataType="{x:Type viewmodel:CTAAddressViewModel}">
        <views:CTAAddress />
    </DataTemplate>
    <DataTemplate x:Key="PremiseTemplate" DataType="{x:Type viewmodel:CTAPremiseViewModel}">
        <views:CTAPremise  />
    </DataTemplate>
</Window.Resources>
<Window.DataContext>
    <viewmodel:CTALightViewModel />
</Window.DataContext>

Content Control

<ContentControl x:Name="ViewSwap" Content="{Binding }">
    <ContentControl.Style>
       <Style TargetType="{x:Type ContentControl}">
           <Style.Triggers>
              <DataTrigger Binding="{Binding ElementName=SearchOptions, Path=IsSelected}" Value="0">
                   <Setter Property="ContentTemplate" Value="{StaticResource AddressTemplate}" />
              </DataTrigger>
              <DataTrigger Binding="{Binding ElementName=SearchOptions, Path=IsSelected}" Value="1">
                  <Setter Property="ContentTemplate" Value="{StaticResource PremiseTemplate}" />
              </DataTrigger>                  
          </Style.Triggers>
       </Style>
   </ContentControl.Style>
</ContentControl>

TextBox To Display

<TextBox Text="{Binding ElementName=ViewSwap, Path=?????, Mode=OneWay}" />

Solution

  • I realized that I needed to implement a view model base to achieve this. I have created one and had the other view models inherit from this base.