我有按钮的自定义样式
<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
答案 0 :(得分:2)
只需使用TemplateBinding
将TextBlock
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>