wpf 如何将 IsHitTestVisible 设置为 True 将其设置为 false 的某个子项?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12148286/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 05:07:03  来源:igfitidea点击:

How can you set IsHitTestVisible to True on a child of something where it's set to false?

wpflayouthittest

提问by Mark A. Donohoe

I have a message which is within a border and is displayed in front of a MDI client area. Because it's just a message, I set it's IsHitTestVisibleto false. For convenience, I've also included a button within it so the user can simply click the button to start a new diagram.

我有一条消息,它位于边框内并显示在 MDI 客户区前面。因为它只是一条消息,所以我将其设置IsHitTestVisible为 false。为方便起见,我还在其中包含了一个按钮,以便用户只需单击该按钮即可开始新图表。

However, because the border has its IsHitTestVisibleset to False, it shorts out the Truevalue of the button so the user can't click on the button.

但是,由于边框已IsHitTestVisible设置为False,因此它会缩短True按钮的值,因此用户无法单击该按钮。

That said, how can you make a control invisible to the mouse, while still allowing one of its children to receive mouse events?

就是说,您如何才能使鼠标不可见控件,同时仍允许其子项之一接收鼠标事件?

Here's a screenshot:

这是一个屏幕截图:

enter image description here

在此处输入图片说明

We want the blue area with text to be hit-test invisible, but we want the button to still be able to be pressed. Thoughts? (And yes, I already know about stacking the two items in a grid, but that messes with the layout.

我们希望带有文本的蓝色区域在命中测试中不可见,但我们希望按钮仍然能够被按下。想法?(是的,我已经知道将这两个项目堆叠在一个网格中,但这会影响布局。

采纳答案by Mark A. Donohoe

Ok, doesn't look like the question can be solved as asked, and therefore it looks like we have to revert to the layered method. In that case, to compensate for text changes, you have to set the vertical alignment of the button to Bottom, and ensure there's extra padding on the bottom edge of the border or bottom margin on the TextBlock) to ensure the label always floats above the button. You manage the side margins/padding for both using the same principles.

好吧,看来问题不能按要求解决了,因此看来我们必须恢复到分层方法。在这种情况下,为了补偿文本更改,您必须将按钮的垂直对齐设置为底部,并确保在边框的底部边缘或 TextBlock 的底部边距上有额外的填充)以确保标签始终浮动在按钮。您使用相同的原则管理两者的边距/填充。

This will give us what we're after, but just seems pretty cumbersome. Again, I wish there was some way to specify IsHitTestVisible (or IsEnabled for that matter) with a 'Force' somehow that re-establishes them (and below) as the owner of the behaviors. A person can dream. Until then... layers it is!

这将为我们提供我们想要的东西,但看起来很麻烦。同样,我希望有某种方法可以指定 IsHitTestVisible(或 IsEnabled)与“Force”以某种方式将它们(以及以下)重新建立为行为的所有者。一个人可以做梦。在那之前......它是层数!

回答by Erez

AFAIK you can't. you can do it however with IsEnabledProperty, and still with a little workaround. you have to create your own button like this:

AFAIK 你不能。但是,您可以使用 IsEnabledProperty 来执行此操作,并且仍然需要一些解决方法。你必须像这样创建自己的按钮:

Edited 9/1/2013 :

2013 年 9 月 1 日编辑:

 public partial class Window1 : Window
{
    public Window1()
    {
        InitializeComponent();
        DataContext = this;
    }

    private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
    {
        MessageBox.Show("hello");
    }
}

public class MyButton : Button
{
    static MyButton()
    {
        IsEnabledProperty.OverrideMetadata(typeof(MyButton),
        new UIPropertyMetadata(true,(o, args) => { },(o, value) => value));
    }
}

XAML:

XAML:

<BorderIsEnabled="False">
    <wpfApplication2:MyButton Margin="61,95,88,100" Click="ButtonBase_OnClick">Open a new diagram</wpfApplication2:MyButton>
</Border>

回答by Basyras

DISCLAIMER:It looks it wont solve exact problem that OP has but i will keep it here because it might be helpful for others with similar issue.

免责声明:看起来它不会解决 OP 的确切问题,但我会将它保留在这里,因为它可能对其他有类似问题的人有所帮助。

Clicking on grid should not register any mouse events on grid.

单击网格不应在网格上注册任何鼠标事件。

<Grid Background="{x:Null}">
   <Button/>
<Grid/>

EDIT: When you want background to be visibile then i would suggest this:For demostration i set cursor="hand" on border

编辑:当您希望背景可见时,我建议这样做:为了演示,我在边框上设置了 cursor="hand"

<Canvas>
    <Border Height="300" Width="300" Background="Red" Cursor="Hand" IsHitTestVisible="False"/>
    <Button Content="click"/>
</Canvas>

or

或者

<Grid>
    <Border  Background="Red" Cursor="Hand" IsHitTestVisible="False" />
    <Button Content="click" HorizontalAlignment="Center"/>
</Grid>

It both cases the trick is that the control (button) is not child of parent with IsHitTestVisible="False" and it is just overlaping the background control

在这两种情况下,诀窍是控件(按钮)不是 IsHitTestVisible="False" 的父级的子级,它只是与背景控件重叠