自定义按钮样式内容属性绑定

时间:2016-02-03 14:26:26

标签: wpf xaml

我有按钮的自定义样式

 <Style x:Key="CustomButton" TargetType="Button">
    <Setter Property="BorderBrush" Value="Black"/>
    <Setter Property="Foreground" Value="White"/>
    <Setter Property="Content">
        <Setter.Value>
            <Grid>
                <Ellipse Width="40" Height="20" Fill="Yellow"/>
                <TextBlock Text="{Binding ****Bind to Content Property on button***}"/>
            </Grid>               
        </Setter.Value>
    </Setter>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Button">
                <Border>
                    <Border.Background>
                        <SolidColorBrush x:Name="CustomBackground"
                                         Color="LightBlue"/>
                    </Border.Background>

                    <ContentPresenter/>
                </Border>                   
            </ControlTemplate>               
        </Setter.Value>
    </Setter>
</Style>

我的Content属性的复杂对象。 content属性的值为TextBlock。是否可以将Text的{​​{1}}属性绑定到Textblock

Content属性
Button

1 个答案:

答案 0 :(得分:2)

只需使用TemplateBindingTextBlock Text绑定到Button Content

    <Style x:Key="CustomButton" TargetType="Button">
        <Setter Property="BorderBrush" Value="Black"/>
        <Setter Property="Foreground" Value="White"/>

        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Border>
                        <Border.Background>
                            <SolidColorBrush x:Name="CustomBackground"
                                     Color="LightBlue"/>
                        </Border.Background>

                        <Grid>
                            <Ellipse Width="40" Height="20" Fill="Yellow"/>
                            <TextBlock Text="{TemplateBinding Content}"/>
                        </Grid>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>