XAML/WPF/C#: How to change the ContentTemplate of a Viewbox programatically

I have a viewbox which points to a static resource "icon-volume" which draws an SVG object using XAML.

I would like to click the viewbox and change the image.

<Viewbox Margin="0,0,5,0" x:Name="ViewboxStartDetectionUnit" MouseLeftButtonDown="ViewboxStartDetectionUnit_MouseLeftButtonDown" Style="{DynamicResource StyleTextViewboxWindowTitle}">
 <ContentPresenter x:Name="ContentStartDetectionUnit" ContentTemplate="{StaticResource icon-volume}"/>
</Viewbox>

Open in new window


How can I change the ContentTemplate of this  Viewbox programatically?
Matt CartlidgeSenior Infrastructure EngineerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Snarf0001Commented:
In this case, you can just cast the .Child as a ContentPresenter, and use .FindResource to get the new one, assuming you have a static resource as a DataTemplate you're looking to change to in the same way:

        private void ViewboxStartDetectionUnit_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            var cp = ((Viewbox)sender).Child as ContentPresenter;
            var rt = this.FindResource("resourceNameOfNewImageTemplate");

            if (cp != null && rt != null && rt is DataTemplate)
            {
                cp.ContentTemplate = (DataTemplate)rt;
            }
        }

Open in new window


This is not doing anything if the ViewBox isn't a ContentPresenter, you get the wrong name for the resource, or the resource isn't a DataTemplate.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Matt CartlidgeSenior Infrastructure EngineerAuthor Commented:
Thanks, works great!
Matt CartlidgeSenior Infrastructure EngineerAuthor Commented:
How do I do the same thing with a button so I can change the contentpresenter template?

<Button x:Name="ButtonStartDetectionUnit" Click="ButtonStartDetectionUnit_Click" Style="{DynamicResource StyleButtonWindowTitleGeneral}">
	<Viewbox Style="{DynamicResource StyleTextViewboxWindowTitle}">
		<ContentPresenter x:Name="ContentStartDetectionUnit" ContentTemplate="{StaticResource icon-volume}"/>
	</Viewbox>
</Button>

Open in new window

Snarf0001Commented:
Either of two ways.  If you're going to reuse the same click handler in other places, then it would be more generic to cast again.
Cast the "sender" to a Button first, then cast the .Child to a Viewbox, then cast the child of that to the ContentPresenter.

But in this case, it looks like you're only planning on using the handler once since it's named specifically for that control.
In that case, since you've already named the ContentPresenter underneath it as well, then just access that directly.

ContentStartDetectionUnit.ContentTemplate = (DataTemplate)rt;
Matt CartlidgeSenior Infrastructure EngineerAuthor Commented:
Awesome, cheers!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
WPF

From novice to tech pro — start learning today.