Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8712007
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T05:01:09+00:00 2026-06-13T05:01:09+00:00

I have a view which contains a ContentControl which has its ContentTemplate changed dynamically

  • 0

I have a view which contains a ContentControl which has its ContentTemplate changed dynamically depending on a boolean property within the view model using a data trigger.

        <ContentControl>

            <!-- MyFirstControl user control by default-->
            <local:MyFirstControl/>

            <ContentControl.Style>
                <Style TargetType="ContentControl">                        
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding IsSelected}"
                                     Value="True">
                            <Setter Property="ContentTemplate">
                                <Setter.Value>
                                    <DataTemplate>
                                        <!-- Different user control when trigger fired-->
                                        <local:MySecondControl />
                                    </DataTemplate>
                                </Setter.Value>
                            </Setter>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </ContentControl.Style>

        </ContentControl>

The MySecondControl user control, which displays when the trigger is fired is intended to display a textblock, of which its Text property binds to a property also within the same view model.

I am obviously wrong, but my thinking was that the triggered control would inherit the same data context. Instead it is trying to use the MyFirstControl user control as its data context (I also receive this error: System.Windows.Data Error: 40 : BindingExpression path error:).

I have tried to explicitly state the data context of the triggered control with:

<local:MySecondControl DataContext="{Binding}"/>

However it is still using the default control (MyFirstControl) as its data context.

My question is, how do I force the triggered control to use the same data context as the view file it is within?

I am fairly new to the WPF scene so I hope this makes sense!

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T05:01:09+00:00Added an answer on June 13, 2026 at 5:01 am

    There’s a difference between Content and ContentTemplate.

    Content is your actual content for the control, while ContentTemplate defines how to draw the Content

    You are setting the Content property to MyFirstControl. Your trigger is changing the ContentTemplate property, so it is changing the way your Content (MyFirstControl) gets drawn so it is drawn using MySecondControl, however the Content itself is unchanged so the DataContext will still be your MyFirstControl.

    You probably want to set the default ContentTemplate to MyFirstControl instead of the actual Content property.

    <ContentControl>
    
        <ContentControl.Style>
            <Style TargetType="ContentControl">  
                <!-- Set default ContentTemplate -->
                <Setter Property="ContentTemplate">
                    <Setter.Value>
                        <DataTemplate>
                            <local:MyFirstControl />
                        </DataTemplate>
                    </Setter.Value>
                </Setter>
    
                <Style.Triggers>
                    <DataTrigger Binding="{Binding IsSelected}"
                                 Value="True">
                        <Setter Property="ContentTemplate">
                            <Setter.Value>
                                <DataTemplate>
                                    <!-- Different user control when trigger fired-->
                                    <local:MySecondControl />
                                </DataTemplate>
                            </Setter.Value>
                        </Setter>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </ContentControl.Style>
    
    </ContentControl>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a view model called ViewModelClass which contains a boolean. I have another
I have a scroll view which has one UIView inside which contains the content.
I have a view which contains 5 columns. Each column has a value of
I have the following view model which contains an array of elements function ReservationsViewModel()
I have a complex view model which contains a collection of other objects which
I have a partial view which contains the following snippet: @model Mbrrace.Domain.MbrraceForm <div class=row>
I am using MVC. I have a view model which contains a Business object.
I have a view that is strongly typed to a view model which contains
i have view controller which contains a button which show the image library ,if
I have an admin view which contains four foreign keys each with a few

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.