Peter
Peter

Reputation: 38555

WPF Custom Control TemplateBinding

How do i define a TemplateBinding for my custom control?

Upvotes: 6

Views: 5956

Answers (2)

Muad'Dib
Muad'Dib

Reputation: 29256

a little somthing like this..... (btw, this xaml is WPF, not silverlight--which is slightly different)

   <style TargetType="{x:Type Button}">
        <Setter Property="Background" Value="Green">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Grid Background={TemplateBinding Background}
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </style>

now, once you apply this style to an object, whenever you set the background of that object, the template will use the Background property (this is a property on the button control) and will be defaulted to what you set in the style (in this case, green)

If you want to use a property that does not exsist on the object of your style, you have to derive your own control and add the property as either a DependencyProperty or use the INotifyPropertyChanged interface. Here is a decent explanation for you.

Upvotes: 5

Charlie
Charlie

Reputation: 15247

Need a bit more information on what you are trying to do. Setting up a TemplateBinding can be done with the following XAML:

{TemplateBinding YourProperty} or

{Binding RelativeSource={RelativeSource TemplatedParent}, Path=YourProperty}

Upvotes: 3

Related Questions