Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting a property of a style's ControlTemplate in XAML

I want to access a property nested inside a style's control template. I know that you can do this in the code-behind:

GradientStop stop = (GradientStop)progressBar1.Template.FindName("gradStop", progressBar1);
stop.Color = Colors.Black;

Is it possible to do the same, but in the XAML? For example:

<ProgressBar Style="{StaticResource CustomProgressBar}" [???].Color="FF000000"/>
like image 797
egelvin Avatar asked Nov 03 '22 01:11

egelvin


1 Answers

Can you not use TemplateBinding?

    <Style x:Key="MyStyle" TargetType="{x:Type ContentControl}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ContentControl}">
                    <Border Width="{TemplateBinding Width}" Height="{TemplateBinding Height}" Background="{TemplateBinding Background}" >
                        <ContentPresenter />
                    </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

Then specify the template bound values when you apply the style.

like image 79
Glyn Richards Avatar answered Dec 23 '22 09:12

Glyn Richards