Template Binding in Control template

templatebinding
wpf custom control template binding
xamarin forms control template
wpf controltemplate
content template wpf
wpf control template templatebinding
wpf controltemplate binding
wpf style template

I have the following control template.

I wish to set the source property for the image control in the control template using Template Binding.

But since this is a control template for button control and the button control doesn't have source property, i can't use TemplateBinding in this case.

<ControlTemplate x:Key="BtnTemplate" TargetType="Button">
        <Border CornerRadius="5"  Margin="15" Cursor="Hand">
            <StackPanel>
                <Image Name="Img" Style="{StaticResource ImageStyle}" Source="temp.jpg" Height="100" Width="100" Margin="5"></Image>
                <Label Content="{TemplateBinding Content}" Background="Transparent" Margin="2"></Label>
            </StackPanel>
        </Border>
    </ControlTemplate>

Since i have to set different images for different instances of button, i can't hardcode the path as well.

Please let me know how to tackle this situation.

I'd suggest using dynamic resources, e.g. define the template as follows:

<ControlTemplate x:Key="buttonTemplate" TargetType="Button">
    <Border CornerRadius="5"  Margin="15" Cursor="Hand">
        <StackPanel Orientation="Horizontal" Background="Yellow">
            <Image Source="{DynamicResource ResourceKey=Img}" Height="100" Width="100" Margin="5"></Image>
            <Label Content="{TemplateBinding Content}" Background="Transparent" Margin="2"></Label>
        </StackPanel>
    </Border>
</ControlTemplate>

And use it like this:

<Button Content="Button" Template="{StaticResource ResourceKey=buttonTemplate}">
    <Button.Resources>
        <ImageSource x:Key="Img">SomeUri.png/</ImageSource>
    </Button.Resources>
</Button>

Template Binding in Control template, I'd suggest using dynamic resources, e.g. define the template as follows: < ControlTemplate x:Key="buttonTemplate" TargetType="Button">� A template binding is a special type of data binding that allows you to reference the parent control, read its properties and apply their values. In some cases, you can use the values directly. In other situations you may need to apply value converters or additional formatting. To demonstrate we need a sample solution.

TemplateBinding is a lightweight "binding", it doesn't support some features of traditional Binding, such as automatically type conversion using the known type converters associated with the target property (such as converting the string URI into a BitmapSource instance).

The following code can work properly:

<Window x:Class="GridScroll.Window2"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window2">
<Window.Resources>
    <Style TargetType="{x:Type Button}" x:Key="ButtonStyle">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Border CornerRadius="5"  Margin="15" Cursor="Hand" Background="Red">
                        <StackPanel Orientation="Horizontal" Background="White">
                            <Image Name="Img" Source="{Binding Path=Tag, RelativeSource={RelativeSource TemplatedParent}}" Margin="5"></Image>
                            <Label Content="{TemplateBinding Content}" Margin="2"></Label>
                        </StackPanel>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>


</Window.Resources>
<StackPanel Orientation="Horizontal">
    <Button Style="{StaticResource ButtonStyle}" Tag="a.jpeg" Content="a"/>
    <Button Style="{StaticResource ButtonStyle}" Tag="b.png" Content="b"/>
</StackPanel>

Xamarin.Forms control templates, The TemplateBinding markup extension binds a property of an element that is in a ControlTemplate to a public property that is defined by the� Using TemplateBinding is a fundamental part of how you define a control template, either if you are a custom control author or if you are replacing a control template for existing controls. For more info, see Quickstart: Control templates. It's fairly common for propertyName and targetProperty to use the same property name. In this case, a

You haven't really said how you expect consumers of your button to set the source. You could use the Button.Tag property, for example, and then bind to that in your template. Or you could define your own control:

public class ImageButton : Button
{
    // add Source dependency property a la Image
}

And then the template:

<ControlTemplate TargetType="ImageButton">
    <Border CornerRadius="5"  Margin="15" Cursor="Hand">
        <StackPanel>
            <Image Name="Img" Style="{StaticResource ImageStyle}" Source="{TempateBinding Source}" Height="100" Width="100" Margin="5"></Image>
            <Label Content="{TemplateBinding Content}" Background="Transparent" Margin="2"></Label>
        </StackPanel>
    </Border>
</ControlTemplate>

Create a template for a control, Learn how to create and reference a control template in Windows The TemplateBinding markup extension binds a property of an element� A TemplateBinding is an optimized form of a Binding for template scenarios, analogous to a Binding constructed with {Binding RelativeSource= {RelativeSource TemplatedParent}, Mode=OneWay}. A TemplateBinding is always a one-way binding, even if properties involved default to two-way binding. Both properties involved must be dependency properties. In order to achieve two-way binding to a templated parent use the following binding statement instead {Binding RelativeSource= {RelativeSource

I'm not sure that I understood your problem very well but why don't you use ContentPresenter? It allows to move the code for your Image at the higher level.

<ControlTemplate x:Key="BtnTemplate" TargetType="Button">
  ...
  <ContentPresenter/>
</ControlTemplate>
...
<Button Template="{StaticResource BtnTemplate}">
  <Image .../>
</Button>

WPF: TemplateBinding with ControlTemplate, Today I'll try to write a bit on TemplateBinding and how to use it inside a ControlTemplate . TemplateBinding is a type of binding used mainly for� Hi guys. Today I will show you how to bind to control properties from ControlTemplate.Let’s say that we have the following template for button: <ControlTemplate TargetType="{x:Type Button}"> <Border x:Name="border"> <ContentPresenter x:Name="contentPresenter"/> </Border> </ControlTemplate> Imagine that you want to set some property for this border using value specified for Button Background

Binding within a ControlTemplate — Xamarin Community Forums, I'm using this template within a page like so: <TemplatedView ControlTemplate=" {StaticResource MyControlTemplate}" />. Assuming my page's� TemplateBinding – allows binding properties in a control template to bindable properties on the parent of the target view that owns the template. The source is always set to the view that owns the template Template 1 <? xml version = "1.0" encoding = "UTF-8" ?>

Binding in a Control Template, Binding in a Control Template. When you're creating a control template and you want to bind to the templated parent you can use: <TextBlock Name="tb"� Binding the BindingContext of the TemplatedView to the Person object in the ViewModel Using TemplateBinding instead of Binding Setting the Source property of the label text binding in the control template to Person

How to bind to control properties from ControlTemplate , Hi guys. Today I will show you how to bind to control properties from ControlTemplate.Let's say that we have the following template for button:� Please also be aware that these templates may be updated at any time, so please be sure that you are using the revision linked here (suffix R2 denotes revision 2). If you want to discuss the templates, or want to help create templates for bindings not listed here, please visit the active discussion here .

Comments
  • Is it possible to use the Button in a much more concise manner? Like <CustomButton Content="Button" Icon="SomeUri.png" /> ? I am struck with this, could you please help me? stackoverflow.com/q/13464735/640607
  • is there any other way to do it rather than having Tag.? I am worried coz, later i might have multiple controls in my control template and i would like to set the properties at the time of creating object. But since Tag is already used, i will not be able use that again.
  • You can do as Kent suggests.Derive from button and create your own control.Add your custom dependency property and use as i use tag
  • My problem was slightly different than the original post, but this was the only solution that did the trick. I spent all morning on this, because errors in a single resource dictionary were preventing the UI from loading in several xaml designers throughout our application. Seriously, thank you.
  • i used tag property but that doesn't work. Source="{TemplateBinding Tag}" and while creating the button, Tag="Temp.jpg" but this doesn't work. i wish to set the source only in XAML no codebehind. Also, Tag might not be a good idea as there can be many controls within the controltemplate for whom i would want to set a few properties at the time of creation of the object.
  • That doesn't work because Source is of type ImageSource, and "Temp.jpg" is a string. You'll need to use a converter to convert strings to ImageSources, much like the Image control does.