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

问题描述 投票:0回答:1

我正在尝试更改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", 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);
        }
    }
}
c# wpf border mouse brush
1个回答
0
投票

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

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

<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>
© www.soinside.com 2019 - 2024. All rights reserved.