我对 Windows 应用程序开发还是新手。
我正在尝试动态创建一个按钮,但我不知道如何删除悬停时的默认背景颜色并添加自定义背景颜色。
在我的
XAML
文件中,我可以轻松地这样做:
<Button
Margin="20"
Height="40"
FontSize="17"
Foreground="White"
x:Name="LoginButton"
Content="LOGIN"
Cursor="Hand">
<Button.Style>
<Style TargetType="{x:Type Button}">
<Setter Property="Background" Value="DarkGreen"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border Background="{TemplateBinding Background}">
<ContentPresenter Margin="{TemplateBinding Padding}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="Green"/>
</Trigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
但是用
c#
来实现这个似乎很难。这是我的代码:
var style = new Style
{
TargetType = typeof(Border),
Setters = { new Setter { Property = Border.CornerRadiusProperty, Value = new CornerRadius(4) } }
};
Button button = new Button();
button.Content = "Click Here";
button.Background = new SolidColorBrush(Colour.red);
button.Foreground = new SolidColorBrush(Colour.white);
button.Resources.Add(style.TargetType, style);
我需要有关如何在将鼠标悬停在按钮上时添加自定义背景颜色的帮助
您必须创建整个样式,然后在您想要的部分应用更改。
转到XAML设计,右键单击控件并编辑模板->编辑为副本。
然后它会生成一个样式代码,您可以更改每个属性。
希望有帮助!
当您发现自己使用 C# 创建控件并将它们添加到 UI 时,您很可能做错了什么或设计不好。在 WPF 中,您通常使用
DataTemplate
动态创建控件或可视内容。不确定
Colour
是什么,但 WPF 已经具有 Colors
类型(例如 Colors.Red
)。
您可以按如下方式修复当前代码(我一般不推荐):
在
App.xaml中定义
Style
(一般情况下,避免在 C# 中构建样式和模板。如果必须,建议的方法是反序列化 XAML 字符串)。ControlTemplate
中的静态颜色。然后在 C# 中创建元素并从应用程序的资源字典中检索 Style
并将其分配给该元素:
应用程序.xaml
<Style TargetType="{x:Type Button}">
<Setter Property="Background" Value="DarkGreen"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border x:Name="Border"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
Padding="{TemplateBinding Padding}">
<ContentPresenter HorizontalAlignment="Center"
VerticalAlignment="Center"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="Border"
Property="Background"
Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(local:FrameworkElementHelper.MouseOverBrush)}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
FramwworkElementHelper.cs
public class FrameworkElementHelper : DependencyObject
{
public static Brush GetMouseOverBrush(DependencyObject attachingElement)
=> (Brush)attachingElement.GetValue(MouseOverBrushProperty);
public static void SetMouseOverBrush(DependencyObject attachingElement, Brush value)
=> attachingElement.SetValue(MouseOverBrushProperty, value);
public static readonly DependencyProperty MouseOverBrushProperty = DependencyProperty.RegisterAttached(
"MouseOverBrush",
typeof(Brush),
typeof(FrameworkElementHelper),
new PropertyMetadata(default(Brush), OnMouseOverBrushChanged));
private static void OnMouseOverBrushChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (e.NewValue is Brush newBrush && newBrush.CanFreeze)
{
newBrush.Freeze();
}
}
}
C#
Brush backgroundBrush = new SolidColorBrush(Colors.Red);
backgroundBrush.Freeze();
Brush foregroundBrush = new SolidColorBrush(Colors.White);
foregroundBrush.Freeze();
// Or better use the Brushes type to get a default colored SolidColorBrush
// that is also already frozen
Brush backgroundBrush = Brushes.Red;
Brush foregroundBrush = Brushes.White;
Button button = new Button()
{
Content = "Click Here",
Background = backgroundBrush,
Foreground = foregroundBrush
};
Brush mouseOverBrush = Brushes.Green;
FrameworkElementHelper.SetMouseOverBrush(button, mouseOverBrush);
XAML
<Button Content="Click Here"
Background="Red"
Foreground="White"
FrameworkElementHelper.MouseOverBrush="Green" />