views:

103

answers:

2

How do i define a TemplateBinding for my custom control?

A: 

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}

Charlie
where do i create the source of this binding and the default value?
Petoj
+2  A: 

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.

Muad'Dib
is that all there is to it? if i want to create my own i just use a setter with a unique name?
Petoj
the property must exsist on the object, either as a DependencyProperty or as an INotifyPropertyChanged property. In this case, Background is a basic property of most controls.
Muad'Dib