根据鼠标状态和位置设置Border.Brush属性

我正在尝试更改UserControl的Border.BorderBrush属性,具体取决于鼠标是否进入,离开或按下了UserControl。我尝试在后面的代码中明确地执行此操作,但是只要Border.BorderBrush属性发生更改,边框就会消失。

我失败了很多可能的解决方案。从当前的代码库开始,我尝试使用样式和触发器来为我管理它。我的问题是,除非您正在处理Button(至少是我从阅读中学到的东西),否则IsMouseDown没有任何属性,因此我为此定义了一个属性。

仅当我认为它可以工作时,边框找不到在UserControl.Resources中定义的样式。

我已经精疲力尽,我不知道该做什么,任何帮助将不胜感激。

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",FrameworkPropertyMetadataOptions.Affectsrender));
        public string SubText
        {
            get
            {
                return (string)Getvalue(SubTextProperty);
            }
            set
            {
                Setvalue(SubTextProperty,value);
            }
        }

        public static readonly DependencyProperty BrushNormalProperty = DependencyProperty.Register("BrushNormal",typeof(Brush),new FrameworkPropertyMetadata(brushes.LightSlateGray));
        public Brush BrushNormal
        {
            get
            {
                return (Brush)Getvalue(BrushNormalProperty);
            }
            set
            {
                Setvalue(BrushNormalProperty,value);
            }
        }

        public static readonly DependencyProperty BrushHoverProperty = DependencyProperty.Register("BrushHover",new FrameworkPropertyMetadata(brushes.LightSteelBlue));
        public Brush BrushHover
        {
            get
            {
                return (Brush)Getvalue(BrushHoverProperty);
            }
            set
            {
                Setvalue(BrushHoverProperty,value);
            }
        }

        public static readonly DependencyProperty BrushDownProperty = DependencyProperty.Register("BrushDown",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),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),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),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,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);
        }
    }
}
mz1bw6l16 回答:根据鼠标状态和位置设置Border.Brush属性

我知道了。原始帖子具有最终的工作代码。

布局应如下所示,而无需显式设置边框的大小:

<Border x:Name="key" Background="Transparent" 
    BorderBrush="{Binding BorderBrush,ElementName=OSKuwuDefaultKey}"
    CornerRadius="{Binding CornerRadius,ElementName=OSKuwuDefaultKey}" 
    BorderThickness="{Binding OutlineThickness,ElementName=OSKuwuDefaultKey}"
    MouseEnter="Key_MouseEnter"
    MouseLeave="Key_MouseLeave"
    MouseDown="Key_MouseDown"
    MouseUp="Key_MouseUp">

    <Canvas>
        ...
    </Canvas>
</Border>
本文链接:https://www.f2er.com/2775341.html

大家都在问