XAML - DataTemplate inheritance

Hi,

  I have this DataTemplate :

    <DataTemplate x:Key="CommandsTemplate">
        <ItemsControl IsTabStop="False" ItemsSource="{Binding}" Margin="6,2">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Button Margin="4 0 0 0" Style="{StaticResource LinkButton}" Command="{Binding Path=Command}">
                        <StackPanel Orientation="Horizontal">
                            <Image Source="{Binding Path=DisplayImage}" VerticalAlignment="Bottom" />
                            <TextBlock Margin="4 0 0 0" FontSize="14" Text="{mvvm:DynamicResourceBinding DisplayName}"/>
                        </StackPanel>
                    </Button>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </DataTemplate>

I need to have a second DataTemplate that is exactly the same, except it adds a "CommandParameter" to the button's command. Like this :

    <DataTemplate x:Key="CommandsTemplate2">
        <ItemsControl IsTabStop="False" ItemsSource="{Binding}" Margin="6,2">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Button Margin="4 0 0 0" Style="{StaticResource LinkButton}" Command="{Binding Path=Command}" CommandParameter="{...blablabla...}">
                        <StackPanel Orientation="Horizontal">
                            <Image Source="{Binding Path=DisplayImage}" VerticalAlignment="Bottom" />
                            <TextBlock Margin="4 0 0 0" FontSize="14" Text="{mvvm:DynamicResourceBinding DisplayName}"/>
                        </StackPanel>
                    </Button>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </DataTemplate>

How can I do this without rewriting the entire XAML code of the first DataTemplate into the second one? Because if I modify the style, I will have to modify in both DataTemplates instead of changing in only one of them. Can I inherit from the first DataTemplate and only add the "CommandParameter" to my second DataTemplate?

Thanks,
Claude
jettaa2vr6Asked:
Who is Participating?
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.

Chinmay PatelChief Technical NinjaCommented:
Hi Claude,

What you are trying to achieve is called ControlTemplate. I think your question title shall be changed to reflect that. Many experts might be thrown off due to the title.

Please study these 2 articles:
1. https://blogs.msdn.microsoft.com/jitghosh/2007/12/27/wpf-control-templates-an-overview/
2. https://www.wpftutorial.net/templates.html

Also what you are trying to do looks a LOT like to an Image Button control, I suggest you should take that route unless and until you are trying to learn WPF and ControlTemplate is the topic. :)

Here is a great article that shows how you can build Image Button.
https://blogs.msdn.microsoft.com/knom/2007/10/31/wpf-control-development-3-ways-to-build-an-imagebutton/

Please note these articles are old but still their logic applies. Let me know if you run into any issues while implementing them.

PS: I think your control template is simple and it should work without any issues but if I have guessed your requirements right, I think a custom control is the right way to go about this.

Regards,
Chinmay.
0

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
jettaa2vr6Author Commented:
Thanks, that did it.
0
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
xaml

From novice to tech pro — start learning today.