简体   繁体   English

ControlTemplate使用控件属性

[英]ControlTemplate use control property

Is it possible for the ControlTemplate to use a property from the control, that uses the template? ControlTemplate是否可以使用控件中的使用模板的属性?

For example, I've got a button, that changes color to Red on MouseOver. 例如,我有一个按钮,它在MouseOver上将颜色更改为红色。 But, I've also got a button, that looks exactly the same, except it changes to White, instead of Red. 但是,我还有一个按钮,它看起来完全一样,只不过它变成了白色而不是红色。 Would it be possible, that whatever Background value the Button has, that value is then used in the control template? 不管Button的背景值是多少,然后可以在控制模板中使用该值?

Currently, this is what my ControlTemplate looks like: 当前,这是我的ControlTemplate的样子:

<ControlTemplate x:Key="CloseButtonTemplate" TargetType="{x:Type Button}">
        <Border>
            <Border.Style>
                <Style>
                    <Setter Property="Border.Background" Value="Transparent"/>
                    <Style.Triggers>
                        <Trigger Property="Border.IsMouseOver" Value="True">
                            <Setter Property="Border.Background" Value="#FFE53935" />
                            <Setter Property="Window.Cursor" Value="Hand" />
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </Border.Style>
            <TextBlock Foreground="#FFEEEEEE" HorizontalAlignment="Center" VerticalAlignment="Center" Text="X" FontFamily="Calibri" />
        </Border>
</ControlTemplate>

What I'm trying to do, is have Border.Background set to whatever the Button Background value is. 我想做的是将Border.Background设置为Button Background值。 So if I have a <Button Background="Red" /> , then the value of Border.Background is Red. 因此,如果我有一个<Button Background="Red" /> ,那么Border.Background的值为Red。

You should probably do this: 您可能应该这样做:

<Border x:Name="border" Background="{TemplateBinding Background}">

See TemplateBinding on MSDN 请参阅MSDN上的TemplateBinding

Yes It is possible for the ControlTemplate to use a property from the control, that uses the template.See this link for binding 是是ControlTemplate可以使用控件中的使用模板的属性。请参阅此链接进行绑定


   <Window.Resources>
    <ControlTemplate x:Key="CloseButtonTemplate" TargetType="{x:Type Button}">
        <Border BorderBrush="Black" BorderThickness="1"  Height="{TemplateBinding Height}" Width="{TemplateBinding Width}">
            <Border.Style>
                <Style>
                    <Setter Property="Border.Background" Value="Transparent"/>
                    <Style.Triggers>
                        <Trigger Property="Border.IsMouseOver" Value="True">
                            <Setter Property="Border.Background" Value="{Binding Path=Background,RelativeSource={RelativeSource TemplatedParent}}" />
                            <Setter Property="Window.Cursor" Value="Hand" />
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </Border.Style>
            <TextBlock Foreground="#FFEEEEEE" HorizontalAlignment="Center" VerticalAlignment="Center" Text="X" FontFamily="Calibri" />
        </Border>
    </ControlTemplate>
</Window.Resources>
<UniformGrid>
    <Button Background="Red" Template="{StaticResource CloseButtonTemplate}"  Height="30" Width="200"/>
    <Button Background="Green" Template="{StaticResource CloseButtonTemplate}"  Height="30" Width="200"/>
    <Button Background="Blue" Template="{StaticResource CloseButtonTemplate}"  Height="30" Width="200"/>
</UniformGrid>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM