【问题标题】:ControlTemplate use control propertyControlTemplate 使用控件属性
【发布时间】:2014-11-29 16:49:45
【问题描述】:

ControlTemplate 是否可以使用控件中使用模板的属性?

例如,我有一个按钮,它在 MouseOver 上将颜色更改为红色。但是,我还有一个按钮,看起来完全一样,只是它变成了白色,而不是红色。是否有可能,无论 Button 具有什么 Background 值,然后在控件模板中使用该值?

目前,这是我的 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>

我想要做的是将Border.Background 设置为Button 背景值。所以如果我有一个&lt;Button Background="Red" /&gt;,那么 Border.Background 的值为 Red。

【问题讨论】:

    标签: c# wpf xaml


    【解决方案1】:

    是的,ControlTemplate 可以使用控件中的属性,该属性使用模板。请参阅此link 进行绑定


       <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>
    

    【讨论】:

    • 这就是我要找的。谢谢!
    【解决方案2】:

    你应该这样做:

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

    TemplateBinding on MSDN

    【讨论】:

      猜你喜欢
      • 2019-06-17
      • 1970-01-01
      • 2011-12-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-20
      • 1970-01-01
      相关资源
      最近更新 更多