如何使用MouseEnter和MouseLeave WPF使对象更改颜色

时间:2013-12-03 20:56:51

标签: c# wpf mouseenter mouseleave

这是我的WPF项目,问题出在这个用户控件People.xaml.cs文件中:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;

namespace Projet
{
public partial class People : UserControl
{
    Random rnd = new Random();
    string filename = "C:\\Users\\Kristen\\peoples.txt";
    public People()
    {
        InitializeComponent();
        dataFromFile();
    }

 void dataFromFile()
    {
        if (!File.Exists(filename))
            return;

        var source = File.ReadLines(filename)
                         .Select(line => line.Split(' '))
                         .Select(m => new { name = m[0], length = int.Parse(m[1]) })
                         .OrderBy(x => x.length);
        int k = 200;
        foreach (var data in source)
        {

            Rectangle r = new Rectangle ();
            r.Height = data.length;
            r.Width = 25;
            r.Fill = new SolidColorBrush(Colors.Red);
            Canvas.SetLeft(r, k);
            Canvas.SetBottom(r, 200);
            board.Children.Add(r);
            k += 50;
        }
    }
    private void board_MouseEnter_1(object sender, MouseEventArgs e)
    {
        board.Background = Brushes.Blue; 
//I have tried with r.Fill = new SolidColorBrush(Colors.Red); but it wont work either
        dataFromFile();
    }

    private void juur_MouseLeave_1(object sender, MouseEventArgs e)
    {
        board.Background = Brushes.Yellow;
    }

IT将数据从文件中删除。在文件中有人名和他们的高度。它使用画布上的矩形将这些高度变为条形图。我想让MouseEnter和MouseLeave改变矩形的颜色,但它只是改变我的画布背景颜色。我会在这里发布我的People.xaml以防万一。

<UserControl x:Class="Project.People"
         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" 
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
<Canvas Background="Yellow" x:Name="board" MouseEnter="board_MouseEnter_1" MouseLeave="board_MouseLeave_1">


</Canvas>

2 个答案:

答案 0 :(得分:1)

您必须将事件处理程序附加到每个Rectangle,而不是Canvas。在这里删除它们:

<Canvas Background="Yellow" x:Name="board"/>

然后将它们添加到每个Rectangle:

foreach (var data in source)
{
    var r = new Rectangle();
    r.MouseEnter += Rectangle_MouseEnter;
    r.MouseLeave += Rectangle_MouseLeave;
    ...
}

...

private void Rectangle_MouseEnter(object sender, MouseEventArgs e)
{
    ((Rectangle)sender).Fill = Brushes.Blue; 
}

private void Rectangle_MouseLeave(object sender, MouseEventArgs e)
{
    ((Rectangle)sender).Fill = Brushes.Yellow;
}

说过我强烈建议丢弃所有代码并使用MVVM方法代替。也许开始阅读ItemsControlData Templating

答案 1 :(得分:0)

绝对没有理由为此使用后台代码,只需使用样式和IsMouseOver触发器(尽管在yoru情况下显然会更改setter以修改/替换相应的子控件属性):

<Canvas>
    <Canvas.Style>
        <Style>
            <Setter Property="Canvas.Background" Value="Yellow"/>
            <Style.Triggers>
                <Trigger Property="Canvas.IsMouseOver" Value="True">
                    <Setter Property="Canvas.Background" Value="Red" />
                </Trigger>
            </Style.Triggers>
        </Style>
    </Canvas.Style>
</Canvas>