簡體   English   中英

根據鼠標狀態和位置設置 Border.Brush 屬性

[英]Set Border.Brush property according to Mouse State and Position

我正在嘗試根據鼠標進入、離開或關閉我的 UserControl 來更改我的 UserControl 的 Border.BorderBrush 屬性。 我已經嘗試在后面的代碼中明確地執行此操作,但是無論何時更改 Border.BorderBrush 屬性,邊框都會消失。

我嘗試了一系列可能的解決方案,但無濟於事。 從我當前的代碼庫開始,我正在嘗試使用樣式和觸發器來為我管理它。 我的問題是 IsMouseDown 沒有屬性,除非您正在處理 Button(至少這是我從閱讀中收集到的),所以我為此定義了一個屬性。

就在我認為它會起作用的時候,邊框找不到我在 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);
        }
    }
}

我讓它工作了。 原始帖子具有最終的工作代碼。

布局應該是這樣的,沒有明確設置邊框的大小:

<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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM