Link to home
Start Free TrialLog in
Avatar of esc_toe_account
esc_toe_accountFlag for United States of America

asked on

XAML TemplateBinding to a Dependency Property

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

Avatar of k_swapnil
k_swapnil
Flag of India image

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...
Avatar of esc_toe_account

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of esc_toe_account
esc_toe_account
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial