首页 新闻 赞助 找找看

wpf 在用户自定义控件中,Image控件不响应鼠标事件

0
悬赏园豆:20 [待解决问题]

我做了一个用户自定义控件:

<UserControl x:Class="AForge.Controls1.WPFVideoSourcePlayer"
             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"
             BorderBrush="Gray"
             BorderThickness="5"
             PreviewMouseLeftButtonUp="UserControl_PreviewMouseLeftButtonUp"
             Loaded="UserControl_Loaded" 
             Focusable="True">
    <Grid>
        <Image Name="imageBox" Stretch="Fill" PreviewMouseDown="imageBox_PreviewMouseDown"/>
    </Grid>
</UserControl>

 

  private void imageBox_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {
            BorderBrush = System.Windows.Media.Brushes.Green;
        }

 

        private void UserControl_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            BorderBrush = System.Windows.Media.Brushes.Green;
        }

 

想让这个控件在点击的时候边框编程绿色.但是无论怎么点击控件中间 都没有反映,但是点击控件边缘的时候有反应,难道Image控件不能响应鼠标事件??

各位高手讲解下~~

wpf
xiaxia—博客园的主页 xiaxia—博客园 | 初学一级 | 园豆:128
提问于:2013-04-04 18:39
< >
分享
所有回答(2)
0

这种功能应该用触发器去实现,最简单的例子可以参考 http://wpftutorial.net/Triggers.html

陈希章 | 园豆:2538 (老鸟四级) | 2013-04-05 07:54

问题不在于此,触发器的机制和使用事件其实是一样的,只是触发器使用样式或者模版的形式定义,编译器在后来为你生了相关的事件.我这样定义个控件,当鼠标移到控件上式然后没有预期的效果:

<UserControl x:Class="IntelligentCameraCenter.TestPlayer"
             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">
    <Grid Background="AliceBlue">
        <Grid.Style>
            <Style TargetType="Grid">
                <Style.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="Background" Value="Red" />
                    </Trigger>
                </Style.Triggers>
            </Style>
        </Grid.Style>
            
    </Grid>
</UserControl>
支持(0) 反对(0) xiaxia—博客园 | 园豆:128 (初学一级) | 2013-04-06 09:58

问题不在于此,触发器的机制和使用事件其实是一样的,只是触发器使用样式或者模版的形式定义,编译器在后来为你生了相关的事件.我这样定义个控件,当鼠标移到控件上式然后没有预期的效果:

<UserControl x:Class="IntelligentCameraCenter.TestPlayer"
             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">
    <Grid Background="AliceBlue">
        <Grid.Style>
            <Style TargetType="Grid">
                <Style.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="Background" Value="Red" />
                    </Trigger>
                </Style.Triggers>
            </Style>
        </Grid.Style>
            
    </Grid>
</UserControl>

我把image放在一个button的content里面 用button来响应鼠标,可以解决我的需求,但是总感觉怪怪的,先暂时不结贴,坐等更高手的回复~~~

支持(0) 反对(0) xiaxia—博客园 | 园豆:128 (初学一级) | 2013-04-06 10:00
-1

image有这个BorderBrush属性吗??瞎写什么呢?

Inspirationss | 园豆:224 (菜鸟二级) | 2013-04-07 10:26

不要激动,BorderBrush是UserControl的~~不是image的

支持(0) 反对(0) xiaxia—博客园 | 园豆:128 (初学一级) | 2013-04-07 10:28

@xiaxia—博客园: 那你就在用户控件里,把image的事件 注册为这个用户控件的路由事件吧。

支持(0) 反对(0) Inspirationss | 园豆:224 (菜鸟二级) | 2013-04-07 14:33
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册