首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >根据鼠标状态和位置设置Border.Brush属性

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

Stack Overflow用户
提问于 2020-01-14 04:41:16
回答 1查看 55关注 0票数 0

我正在尝试更改我的UserControl的Border.BorderBrush属性,这取决于鼠标在我的UserControl上是进入、离开还是关闭。我曾尝试在后台代码中显式地执行此操作,但只要Border.BorderBrush属性发生更改,边框就会消失。

我尝试了一系列可能的解决方案,但都没有效果。在我目前的代码库中,我尝试使用样式和触发器来管理它。我的问题是,除非你正在处理一个按钮,否则没有IsMouseDown的属性(至少这是我从阅读中得到的),所以我为它定义了一个属性。

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

我已经用尽了我所能做的一切,任何帮助都会非常感谢。

XAML:

代码语言:javascript
复制
<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>

代码隐藏:

代码语言:javascript
复制
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);
        }
    }
}
EN

回答 1

Stack Overflow用户

发布于 2020-01-14 06:12:42

我把它修好了。最初的文章有最终的工作代码。

布局应该看起来像这样,没有显式设置边框的大小:

代码语言:javascript
复制
<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>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59723862

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档