【发布时间】:2016-06-30 20:27:28
【问题描述】:
我找到了这段代码: XAML:
<Button Content="Click Me!" Margin="209,135,263.4,140.8" Background="{x:Null}" BorderBrush="{x:Null}" Click="Button_Click_1">
<Button.Template>
<ControlTemplate TargetType="{x:Type Button}">
<Border x:Name="bdr_main" CornerRadius="3" Margin="0" BorderThickness="1" BorderBrush="Black" Background="LightGray">
<ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center" Margin="8,6,8,6" ContentSource="Content" />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="bdr_main" Property="Background" Value="LightGreen"/>
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter TargetName="bdr_main" Property="Background" Value="Red"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Button.Template>
</Button>
可以用 WTF 代码改变背景颜色吗? 我点击按钮,背景颜色发生变化。
使用普通按钮 我可以使用这段代码: bdr_main.Background = new SolidColorBrush(Colors.Red); 或者 bdr_main.Background = 画笔.Red; 但他不工作 你能帮帮我吗?
【问题讨论】:
-
更改边框以使用 TemplateBinding 而不是 Background="LightGray"
-
对不起,您的代码运行良好,谢谢您有与 WPF 代码相同的解决方案吗?
-
你的 Xaml 看起来像 WPF ???没有。
-
是的,但是像一些类似的例子:bdr_main.Background = new SolidColorBrush(Colors.Red);我会有更好的掌握