【发布时间】:2020-01-13 20:41:16
【问题描述】:
我正在尝试更改我的 UserControl 的 Border.BorderBrush 属性,具体取决于鼠标是进入、离开还是在我的 UserControl 上按下。我已经尝试在后面的代码中明确地这样做,但是只要更改 Border.BorderBrush 属性,边框就会消失。
我尝试了一系列可能的解决方案,但均无济于事。在我当前的代码库中,我正在尝试使用 Style 和 Triggers 来为我管理它。我的问题是 IsMouseDown 没有属性,除非您正在处理一个 Button(至少这是我从阅读中收集到的),所以我为此定义了一个属性。
就在我认为它会起作用的时候,边框找不到我在 UserControl.Resources 中定义的 Style。
我已经用尽了我所知道的一切,任何帮助将不胜感激。
XAML:
<UserControl x:Class="OSK.Resources.Themes.Default.Key"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:OSK.Resources.Themes.Default"
mc:Ignorable="d"
x:Name="OSKuwuDefaultKey"
d:DesignHeight="48" d:DesignWidth="48">
<UserControl.Style>
<Style x:Name="KeyStyle">
<Style.Triggers>
<Trigger Property="Border.IsMouseOver" Value="true">
<Setter Property="Border.BorderBrush" Value="{Binding Path=BrushHover, RelativeSource={RelativeSource Self}}" />
</Trigger>
<Trigger Property="Border.IsMouseOver" Value="false">
<Setter Property="Border.BorderBrush" Value="{Binding Path=BrushNormal, RelativeSource={RelativeSource Self}}" />
</Trigger>
<Trigger Property="local:Key.IsMouseDown" Value="true">
<Setter Property="Border.BorderBrush" Value="{Binding Path=BrushDown, RelativeSource={RelativeSource Self}}" />
</Trigger>
</Style.Triggers>
</Style>
</UserControl.Style>
<Border x:Name="key" Background="Transparent" Width="{Binding Path=Width, ElementName=OSKuwuDefaultKey}" Height="{Binding Path=Height, ElementName=OSKuwuDefaultKey}" BorderBrush="{Binding BorderBrush, ElementName=OSKuwuDefaultKey}" CornerRadius="{Binding Path=CornerRadius, ElementName=OSKuwuDefaultKey}" BorderThickness="{Binding Path=OutlineThickness, ElementName=OSKuwuDefaultKey}" MouseEnter="Key_MouseEnter" MouseLeave="Key_MouseLeave" MouseDown="Key_MouseDown" MouseUp="Key_MouseUp">
<Canvas>
<Label Content="{Binding SuperText, ElementName=OSKuwuDefaultKey}" Canvas.Top="6" Canvas.Left="8" />
<Label Content="{Binding SubText, ElementName=OSKuwuDefaultKey}" Canvas.Top="20" Canvas.Right="4" />
</Canvas>
</Border>
代码隐藏:
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
namespace OSK.Resources.Themes.Default
{
/// <summary>
/// Interaction logic for Key.xaml
/// </summary>
public partial class Key : UserControl
{
public static readonly DependencyProperty SuperTextProperty = DependencyProperty.Register("SuperText", typeof(string), typeof(Key), new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.AffectsRender));
public string SuperText
{
get
{
return (string)GetValue(SuperTextProperty);
}
set
{
SetValue(SuperTextProperty, value);
}
}
public static readonly DependencyProperty SubTextProperty = DependencyProperty.Register("SubText", typeof(string), typeof(Key), new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.AffectsRender));
public string SubText
{
get
{
return (string)GetValue(SubTextProperty);
}
set
{
SetValue(SubTextProperty, value);
}
}
public static readonly DependencyProperty BrushNormalProperty = DependencyProperty.Register("BrushNormal", typeof(Brush), typeof(Key), new FrameworkPropertyMetadata(Brushes.LightSlateGray));
public Brush BrushNormal
{
get
{
return (Brush)GetValue(BrushNormalProperty);
}
set
{
SetValue(BrushNormalProperty, value);
}
}
public static readonly DependencyProperty BrushHoverProperty = DependencyProperty.Register("BrushHover", typeof(Brush), typeof(Key), new FrameworkPropertyMetadata(Brushes.LightSteelBlue));
public Brush BrushHover
{
get
{
return (Brush)GetValue(BrushHoverProperty);
}
set
{
SetValue(BrushHoverProperty, value);
}
}
public static readonly DependencyProperty BrushDownProperty = DependencyProperty.Register("BrushDown", typeof(Brush), typeof(Key), new FrameworkPropertyMetadata(Brushes.SlateGray));
public Brush BrushDown
{
get
{
return (Brush)GetValue(BrushDownProperty);
}
set
{
SetValue(BrushDownProperty, value);
}
}
public static readonly DependencyProperty CornerRadiusProperty = DependencyProperty.Register("CornerRadius", typeof(int), typeof(Key), new FrameworkPropertyMetadata(4, FrameworkPropertyMetadataOptions.AffectsRender));
public int CornerRadius
{
get
{
return (int)GetValue(CornerRadiusProperty);
}
set
{
SetValue(CornerRadiusProperty, value);
}
}
public static readonly DependencyProperty OutlineThicknessProperty = DependencyProperty.Register("OutlineThickness", typeof(Thickness), typeof(Key), new FrameworkPropertyMetadata(new Thickness(1), FrameworkPropertyMetadataOptions.AffectsRender));
public Thickness OutlineThickness
{
get
{
return (Thickness)GetValue(OutlineThicknessProperty);
}
set
{
SetValue(OutlineThicknessProperty, value);
}
}
public static DependencyProperty IsMouseDownProperty = DependencyProperty.RegisterAttached("IsMouseDown", typeof(bool), typeof(Key), new FrameworkPropertyMetadata(default(bool)));
public bool IsMouseDown
{
get
{
return (bool)GetValue(IsMouseDownProperty);
}
set
{
SetValue(IsMouseDownProperty, value);
}
}
public Key()
{
InitializeComponent();
}
private void Key_MouseEnter(object sender, MouseEventArgs e)
{
//this.SetValue(Key.BorderBrushProperty, BrushNormal);
//Console.WriteLine(GetValue(Key.BorderBrushProperty));
}
private void Key_MouseLeave(object sender, MouseEventArgs e)
{
//this.SetValue(Key.BorderBrushProperty, BrushHover);
}
private void Key_MouseDown(object sender, MouseButtonEventArgs e)
{
//this.SetValue(Key.BorderBrushProperty, BrushDown);
this.SetValue(Key.IsMouseDownProperty, true);
}
private void Key_MouseUp(object sender, MouseButtonEventArgs e)
{
this.SetValue(Key.IsMouseDownProperty, false);
}
}
}
【问题讨论】:
-
您可以直接分配样式,而不是将样式声明为资源:
<UserControl.Style><Style ...>...</Style></UserControl.Style>。 Setter 中的绑定缺少适当的源对象,例如{Binding Path=BrushHover, RelativeSource={RelativeSource Self}} -
@Clemens 实际上我确实也尝试过 UserControl.Style,但忘了注意这一点。我收到一条错误消息
'Key' TargetType does not match type of element 'UserControl'.所以我尝试将其更改为<local:Key.Style>但后来我得到The member "Style" is not recognized or is not accessible. -
@Clemens 我需要 TargetType Key 的原因是因为我需要访问我的
Key.IsMouseDown属性。 -
您的依赖属性声明不完整。请看一下这个,尤其是实现“包装器”:docs.microsoft.com/en-us/dotnet/framework/wpf/advanced/…
-
@codechinchilla "a solid color" 应该是通常的
Background="Transparent",与默认的null背景不同。