WPF MouseDown事件在控件中无处不在 [英] WPF MouseDown event not firing everywhere in a control

查看:177
本文介绍了WPF MouseDown事件在控件中无处不在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在与另一个WPF斗争进行斗争,即mouseEvents。
我基本上有一个非常简单的控件(一个 Border 包含一个 Grid ,它本身有一些的TextBlocks )。我试图实现一个简单的行为:双击应该将控件转换为编辑模式(实际上隐藏 TextBlocks TextBoxes 绑定到相同的数据。



没有什么花哨,对吗?还是,我很挣扎, MouseDoubleClick 链接到我的 UserControl 只是在我点击控件时触发(例如,点击一个文本框)如果我点击 TextBlocks ,没有任何东西被触发,甚至没有 MouseDown



工作,以便抓住每一个鼠标点击?我假设将 MouseDown 事件链接到边框应该捕获每个点击边框,但...最终没有捕捉边框空白部分的点击。



这是我为您运行的一些草案代码:



XAML:

 < StackPanel Orientation =Vertical> 
< Border Height =400Width =400Horizo​​ntal Alignment =CenterVerticalAlignment =CenterBorderBrush =BlackBorderThickness =2
MouseDown =Border_MouseDownMouseUp =Border_mouseUp>
< Grid>
< Grid.ColumnDefinitions>
< ColumnDefinition Width =*/>
< ColumnDefinition Width =*/>
< ColumnDefinition Width =*/>
< /Grid.ColumnDefinitions>
< Grid.RowDefinitions>
< RowDefinition Height =*/>
< RowDefinition Height =*/>
< RowDefinition Height =*/>
< /Grid.RowDefinitions>

< TextBlock Text =BLUFFGrid.Column =0Grid.Row =0Horizo​​ntalAlignment =CenterVerticalAlignment =Center/>
< TextBlock Text =BLUFFGrid.Column =0Grid.Row =1Horizo​​ntalAlignment =CenterVerticalAlignment =Center/>
< TextBlock Text =BLUFFGrid.Column =0Grid.Row =2Horizo​​ntalAlignment =CenterVerticalAlignment =Center/>
< TextBlock Text =BLUFFGrid.Column =1Grid.Row =0Horizo​​ntalAlignment =CenterVerticalAlignment =Center/>
< TextBlock Text =BLUFFGrid.Column =1Grid.Row =1Horizo​​ntalAlignment =CenterVerticalAlignment =Center/>
< TextBlock Text =BLUFFGrid.Column =1Grid.Row =2Horizo​​ntalAlignment =CenterVerticalAlignment =Center/>
< TextBlock Text =BLUFFGrid.Column =2Grid.Row =0Horizo​​ntalAlignment =CenterVerticalAlignment =Center/>
< TextBlock Text =BLUFFGrid.Column =2Grid.Row =1Horizo​​ntalAlignment =CenterVerticalAlignment =Center/>
< TextBlock Text =BLUFFGrid.Column =2Grid.Row =2Horizo​​ntalAlignment =CenterVerticalAlignment =Center/>
< / Grid>

< / Border>

< TextBlock Height =100Width =300Horizo​​ntalAlignment =CenterTextAlignment =Centerx:Name =thetextVisibility =Collapsed
Foreground =白色背景=红色文本=点击! />
< / StackPanel>

代码背后:

  private void Border_MouseDown(object sender,MouseButtonEventArgs e)
{
thetext.Visibility = Visibility.Visible;
}

private void Border_mouseUp(object sender,MouseButtonEventArgs e)
{
thetext.Visibility = Visibility.Collapsed;
}

现在尝试单击其中一个BLUFF文本:CLICKED文字会出现。尝试点击其他地方,TextBlocks之间:没有发生。



谢谢!

解决方案

 < StackPanel Background =透明... 

 < Border Background =透明... 

应该做的诀窍...



这使得网格透明,鼠标点击。



查看 here 表单更多信息。



还有一些其他的情况,你必须使用 PreviewXXX -Event,这是什么时候一个孩子元素吞下事件,但在你的情况下,上面的shpould你正在寻找。


I am currently fighting against another WPF struggle, namely mouseEvents. I basically have a very simple control (a Border containing a Grid which itself has a few TextBlocks). I am trying to achieve a simple behavior: Double click should turn the control into edit mode (which in fact hides the TextBlocks with TextBoxes bound to the same data.

Nothing fancy, right? Well still, I'm struggling. The MouseDoubleClick linked to my UserControl just fires when I click in a control (like, clicking ON a textblock). If I click on an empty space between TextBlocks, nothing is fired. Not even MouseDown.

How could I make it work so as to catch every mouse click? I assumed that linking a MouseDown event to a Border should catch every click on the border but... it ended up not catching clicks on empty parts of the border.

Here is some draft code I made for you to run it:

XAML:

<StackPanel Orientation="Vertical">
    <Border Height="400" Width="400" HorizontalAlignment="Center" VerticalAlignment="Center" BorderBrush="Black" BorderThickness="2"
        MouseDown="Border_MouseDown" MouseUp="Border_mouseUp">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="*" />
                <RowDefinition Height="*" />
                <RowDefinition Height="*" />
            </Grid.RowDefinitions>

            <TextBlock Text="BLUFF" Grid.Column="0" Grid.Row="0" HorizontalAlignment="Center" VerticalAlignment="Center"/>
            <TextBlock Text="BLUFF" Grid.Column="0" Grid.Row="1" HorizontalAlignment="Center" VerticalAlignment="Center"/>
            <TextBlock Text="BLUFF" Grid.Column="0" Grid.Row="2" HorizontalAlignment="Center" VerticalAlignment="Center"/>
            <TextBlock Text="BLUFF" Grid.Column="1" Grid.Row="0" HorizontalAlignment="Center" VerticalAlignment="Center"/>
            <TextBlock Text="BLUFF" Grid.Column="1" Grid.Row="1" HorizontalAlignment="Center" VerticalAlignment="Center"/>
            <TextBlock Text="BLUFF" Grid.Column="1" Grid.Row="2" HorizontalAlignment="Center" VerticalAlignment="Center"/>
            <TextBlock Text="BLUFF" Grid.Column="2" Grid.Row="0" HorizontalAlignment="Center" VerticalAlignment="Center"/>
            <TextBlock Text="BLUFF" Grid.Column="2" Grid.Row="1" HorizontalAlignment="Center" VerticalAlignment="Center"/>
            <TextBlock Text="BLUFF" Grid.Column="2" Grid.Row="2" HorizontalAlignment="Center" VerticalAlignment="Center"/>
        </Grid>

    </Border>

    <TextBlock Height="100" Width="300" HorizontalAlignment="Center" TextAlignment="Center" x:Name="thetext" Visibility="Collapsed"
               Foreground="White" Background="Red" Text="CLICKED!" />
</StackPanel>

Code behind:

private void Border_MouseDown(object sender, MouseButtonEventArgs e)
    {
        thetext.Visibility = Visibility.Visible;
    }

    private void Border_mouseUp(object sender, MouseButtonEventArgs e)
    {
        thetext.Visibility = Visibility.Collapsed;
    }

Now try to click one of the "BLUFF" texts: A "CLICKED" Text will appear. Try clicking somewhere else, between the TextBlocks: Nothing happens.

Thank you!

解决方案

<StackPanel Background="Transparent" ...

or

<Border Background="Transparent" ...

should do the trick...

This makes the grid transparent, however visible for mouse-clicks.

Look here form more Information.

There are some other cirmumstances, where you have to use the PreviewXXX-Event, this is when a child element "swallows" the event away, but in your case, the above shpould what you're looking for.

这篇关于WPF MouseDown事件在控件中无处不在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆