我想在代码行为文件中设置前景。但是设置Foreground
不会改变视觉样式。字体大小顺便说一句?
<ControlTemplate x:Key="SimpleBtn" TargetType="Button">
<Button x:Name="btnct" CommandParameter="{Binding Content}" Command="{Binding Path=DataContext.ButtonClickCommand,ElementName=MainGrid}"
keyImageProperty:KeyPressed.Image="{DynamicResource ResourceKey=gbutton70x70}" keyImageProperty:KeyNotPressed.Image="{DynamicResource ResourceKey=button70x70}"
Template="{DynamicResource ResourceKey=KeyboardButton}" Content="{TemplateBinding Content}"
FontSize="{TemplateBinding FontSize}" FontWeight="Medium" Foreground="{TemplateBinding Foreground}" FontFamily="{TemplateBinding FontFamily}" />
</ControlTemplate>
<DataTemplate x:Name="btndatatemplate" x:Key="itemContDataTemplate" >
<Button x:Name="btnstandard" Template="{DynamicResource ResourceKey=SimpleBtn}"
FontWeight="Bold"
Content="{Binding Content}" />
</DataTemplate>
答案 0 :(得分:1)
这应该用于在后面的代码中的按钮上设置模板绑定(作为完整ControlTemplate定义的一部分):
var button = new FrameworkElementFactory(typeof(Button)) { Name = "btnct" };
button.SetValue(Button.ForegroundProperty, new TemplateBindingExtension(Button.ForegroundProperty));
FontSize起作用不是因为模板绑定有效,而是因为FontSize由子控件自动继承,而前台则不能。 (在XAML中尝试以下操作:)
<Button FontSize="20" Foreground="Red">
<Button Content="Click"/>
</Button>