首页新闻找找看学习计划

WPF控件样式

0
悬赏园豆:5 [已解决问题] 解决于 2019-10-14 20:22

我写了一个textblock,想在鼠标移入的时候改变背景色,鼠标移出时恢复背景色.现在就是鼠标移入能变颜色,但是鼠标在控件中移动时,背景色又会变成原来的颜色.
下面是控件的样式:
<Style TargetType="TextBlock" x:Key="TextBlock_Close">
<Setter Property="HorizontalAlignment" Value="Center"></Setter>
<Setter Property="VerticalAlignment" Value="Center"></Setter>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="Background" Value="Red"></Setter>
</Trigger>
</Style.Triggers>
</Style>

问题补充:

<Window x:Class="MyClient.Page.Login"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:MyClient.Page"
mc:Ignorable="d"
Title="Login" WindowStyle="None" WindowState="Normal" WindowChrome.WindowChrome="{DynamicResource WindowChromeKey}" WindowStartupLocation="CenterScreen" Width="600" Height="400">
<Window.Resources>
<WindowChrome x:Key="WindowChromeKey">
<WindowChrome.ResizeBorderThickness>
<Thickness>0</Thickness>
</WindowChrome.ResizeBorderThickness>
</WindowChrome>
</Window.Resources>

 

<Grid Grid.Row="0">
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right" VerticalAlignment="Top" Margin="0,10,10,0">
<Button Content="×" Width="30" Height="30" FontSize="20" >
<Button.Style>
<Style TargetType="Button" >
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Grid Width="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Path=Width}" Height="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Path=Height}">
<Rectangle Fill="#5b9dc6" ></Rectangle>
<TextBlock Text="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Path=Content}" HorizontalAlignment="Center" VerticalAlignment="Center"></TextBlock>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="Foreground" Value="White"></Setter>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Grid Width="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Path=Width}" Height="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Path=Height}">
<Rectangle Fill="#3a79a0"></Rectangle>
<TextBlock Text="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Path=Content}" HorizontalAlignment="Center" VerticalAlignment="Center"></TextBlock>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Trigger>
</Style.Triggers>
</Style>

</Button.Style>
</Button>

</StackPanel>


</Grid>

</Window>

请假一天的主页 请假一天 | 初学一级 | 园豆:108
提问于:2019-10-14 16:07

能把完整的XAML代码贴一下吗

黑白条纹 1个月前

@黑白条纹: 已补充

请假一天 1个月前

你的代码在我的计算机上运行正常,不知道你是不是设置了WindowStyle="None",而鼠标移动到边框上导致异常

黑白条纹 1个月前

@黑白条纹: 不是这个问题,应该是控件位置的问题,我之前是放在右上角的,每次在控件上移动鼠标的位置,背景色都会恢复原样,现在我把控件放在页面中间就不会了

请假一天 1个月前

@请假一天: 你把整个文件发一下吧

黑白条纹 1个月前

@黑白条纹: 不清楚注明发文件.. 我把所有代码都贴出来了,只是修改后的,如果把StackPanel 的margin设置为0,30,30,0 也就是button的长和宽,就不会出现这个问题,不然的话,每次移动到一定位置,就算鼠标还在控件上,背景色还是会恢复原样

请假一天 1个月前
< >
分享
最佳答案
-2
class ButtonBg3Status:ButtonNone
    {
        private uint _colorVal0 = ThemeDefine.None;
        private uint _colorVal1 = ThemeDefine.Default.Primary0;
        private uint _colorVal2 = ThemeDefine.Default.Primary1;

        public uint Mask = 0X66FFFFFF;
        public uint ColorVal0
        {
            get => _colorVal0 & Mask;
            set
            {
                _colorVal0 = value;
                Background = ColorVal0.ToColorBrush();
            }
        }

        public uint ColorVal1
        {
            get => _colorVal1 & Mask;
            set { _colorVal1 = value; }
        }

        public uint ColorVal2
        {
            get => _colorVal2 & Mask;
            set { _colorVal2 = value; }
        }
        protected override void OnMouseEnter(MouseEventArgs e)
        {
            base.OnMouseEnter(e);
            Background = ColorVal1.ToColorBrush();
        }

        protected override void OnMouseLeave(MouseEventArgs e)
        {
            base.OnMouseLeave(e);
            Background = ColorVal0.ToColorBrush();
        }

        protected override void OnMouseDown(MouseButtonEventArgs e)
        {
            base.OnMouseDown(e);
            Background = ColorVal2.ToColorBrush();
        }

        protected override void OnMouseUp(MouseButtonEventArgs e)
        {
            base.OnMouseUp(e);
            Background = ColorVal1.ToColorBrush();
        }
    }

代码是最灵活的方式,也是离原理最近的地方,很多时候也是最快的方式。

收获园豆:5
花飘水流兮 | 专家六级 |园豆:10177 | 2019-10-14 16:36

呵呵, 不知道给我减号的是不是 不知道 Template控件的操作还是什么问题。

    public partial class ListBoxEx
    {
        public Func<ListBoxEx, UIElement> OnGenerateItem;
        public int GenerateCounterOnStart { get; private set; }
        public Action<ListBoxEx,DependencyPropertyChangedEventArgs> PropertyChanged { get; set; }
        public ListBoxEx()
        {
            InitializeComponent();
        }

        protected override DependencyObject GetContainerForItemOverride()
        {
            GenerateCounterOnStart++;
            var itemContent = OnGenerateItem?.Invoke(this);
            return itemContent ?? new ListBoxItem(); ;
        }

        public ScrollViewerEx ScrollViewer { get; private set; }
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            ScrollViewer = this.GetTemplateChild("_cScrollViewer") as ScrollViewerEx;
        }

        public new IEnumerable ItemsSource
        {
            get => base.ItemsSource;
            set
            {
                base.ItemsSource = value;
                GenerateCounterOnStart = 0;
            }
        }

        public int MaxItemsCount { get; set; } = 1000;
        public bool IsReduceHead { get; set; }
        protected override void OnItemsChanged(NotifyCollectionChangedEventArgs e)
        {
            base.OnItemsChanged(e);
            while (MaxItemsCount > 0 && Items.Count > MaxItemsCount)
            {
                if (!IsReduceHead)
                {
                    var item = Items[Items.Count - 1];
                    if(item is DependencyObject) item.ToType<DependencyObject>().GetAllChildren().OfType<Image>().ToList().ForEach(t=>t.Source = null);
                    Items.RemoveAt(Items.Count - 1);
                }
                else
                {
                    var item = Items[0];
                    if (item is DependencyObject) item.ToType<DependencyObject>().GetAllChildren().OfType<Image>().ToList().ForEach(t => t.Source = null);
                    Items.RemoveAt(0);
                }
            }
        }

        protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e)
        {
            base.OnPropertyChanged(e);
            PropertyChanged?.Invoke(this,e);
        }

        protected override AutomationPeer OnCreateAutomationPeer()
        {
            return new ControlSpecificCustomAutomationPeer(this);
        }
    }
至于其他GridXX,ListXX动态奇偶行 等等不同渲染,真想给减号的上上课。

 

花飘水流兮 | 园豆:10177 (专家六级) | 2019-10-14 18:12

@花飘水流兮: 谢谢回答,你这个是可行的,我最开始是想过自定义控件的,就是懒,想知道能不能修改样式就实现

请假一天 | 园豆:108 (初学一级) | 2019-10-14 20:22
其他回答(1)
0

MouseEnter,MouseLeave使用这两个试下

活在梦里的鱼 | 园豆:204 (菜鸟二级) | 2019-10-14 16:21

还是不行

支持(0) 反对(0) 请假一天 | 园豆:108 (初学一级) | 2019-10-14 16:26
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册