Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

XAML TemplateBinding to a Dependency Property

Posted on 2010-08-27
3
1,681 Views
Last Modified: 2013-11-27
I have created my own User Control which is based on Button. Inside myButton I want a small icon depicting the type of myButton. If I hardcode the image source in the button template everything works great. But I would like to programmatically/declaratively show different icons inside myButton based on a dependency property which I have created zIcon. The XAML template code which should take care of this is simply:

    <Image Source="{TemplateBinding zIcon}"/>

However, when I run this code I get an immediate error:

    "The Given key was not present in the dictionary"

I assume it is talking about my dependency property zIcon somehow not being in the dictionary? As you can see in the code below, that TemplateBinding technique works great for setting the Content but doesn't work for a custom dependency property. Any suggestions about how to remedy this?

P.S. I have not included the dependency property class/code snippet. Suffice it to say, zIcon shows up just fine in the Visual Studio Properties window for myButton.


<UserControl.Resources>
        <ControlTemplate x:Key="thisTemplate" TargetType="{x:Type Button}">
            <Button x:Name="thisButton" />
            <ControlTemplate.Triggers>
                    <!-- Only a native Button type has IsPressed, myButton does not-->
                <Trigger Property="IsPressed" Value="True">
                    <Setter TargetName="thisButton" Property="Background" Value="LightGray" />
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>

        <Style TargetType="{x:Type uCtrl:myButton}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type uCtrl:myButton}">
                        <Button x:Name="thisButton" Width="100" Height="30" 
                                    BorderBrush="Transparent" Background="Transparent">
                            <Button.Content>
                                <StackPanel Orientation="Horizontal" HorizontalAlignment="Left">
                                    <Image Source="{TemplateBinding zIcon}"/>
                                    <ContentPresenter VerticalAlignment="Center" Content="{TemplateBinding Content}" />
                                </StackPanel>
                            </Button.Content>
                        </Button>
                        <ControlTemplate.Triggers>
                            <!-- This trigger will not work if I place it in the ControlTemplate definition -->
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter TargetName="thisButton" Property="Background" Value="LightCyan" />
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </UserControl.Resources>
    
    <Grid>
        <Button x:Name="btnPageType" Template="{StaticResource thisTemplate}" />
    </Grid>
</UserControl>

Open in new window

0
Comment
Question by:esc_toe_account
  • 2
3 Comments
 
LVL 10

Expert Comment

by:k_swapnil
ID: 33553707
In your usercontrol, make a dependency property say myButtonImage.
Now bind this property to the image source.

While using your custombutton set this property to the image path you wish to display on the button.

I think this would work... I you face any problem just let me know...

Thx!
Swaps...
0
 

Author Comment

by:esc_toe_account
ID: 33557890
Thanks for your suggestion. I guess I should have been more explicit - the property zIcon is already a dependency property (same as myButtonImage would be). And the error I got/posted above is what happens when I implemented the code as you described.
0
 

Accepted Solution

by:
esc_toe_account earned 0 total points
ID: 33558931
I have found the answer. It is to replace:

    <Image Source="{TemplateBinding zIcon}"/>

with

    <Image Source="{Binding Path=zIcon, RelativeSource={RelativeSource TemplatedParent}}"

0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
.net and XML report from SCCM 5 41
vb.net winforms sizing/resolution? 4 42
VB.NET 2008 - SQL Timeout 9 34
Amazon S3 Images with .Net 3 10
Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question