使文本块不透明以进行鼠标点击 - 这样育儿面板的事件就不会触发



好吧,一个人将如何制作它,以便textblock(或其他(Frameworkelement具有点击事件。但是,如果点击事件被解雇,则父母的(网格(点击事件将不会发射。 - 如果未单击文本框/其他。

,父母的点击事件应发射。

这在一个简单的示例中显示:

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
namespace testit
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow() {
            InitializeComponent();
            Grid mainGrid = (Grid)FindName("MainGrid");
            mainGrid.MouseLeftButtonDown += ClickInNowhere;
            TextBlock tb = new TextBlock {
                Text = "hellonworld"
            };
            tb.MaxWidth = 100;
            tb.MaxHeight = 100;
            mainGrid.Children.Add(tb);
            tb.MouseLeftButtonDown += ClickOnBox;
        }
        private void ClickInNowhere(object o, MouseButtonEventArgs args) {
            tb.Text = "hellonworld";
        }
        private void ClickOnBox(object o, MouseButtonEventArgs args) {
            tb.Text += "a";
            //do something so that the "MouseLeftButtonDown" of the main window doesn't fire
        }
    }
}

使用XML格式:

<Window x:Class="testit.MainWindow"
        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:testit"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid x:Name="MainGrid" Background="#FFC8C8C8" />
</Window>

请注意,HTE网格具有使其对鼠标事件做出反应的背景。应该有两种情况:

  • 单击文本框:框中的文本由" a"
  • 附加
  • 单击框外:文本框中的文本重置为" Hello nworld"

这显然不会发生:当通过调试器跑步时,可以看出两个事件都被解雇了。

那么如何使鼠标事件的文本块不透明?

您需要说,该事件已处理:

private void ClickInNowhere(object o, MouseButtonEventArgs args) {
        tb.Text = "hellonworld";
        args.Handled = true;
}

具有处理属性的Routedeventargs的MouseButtoneVentargs derrive,在这里您可以阅读有关它的更多信息:https://msdn.microsoft.com/en-us/library/system.windows.windows.routededeventeventargs.handargs.handarled例(110(.aspx

我也建议阅读有关事件冒泡的信息:https://www.codeproject.com/articles/464926/464926/to-bubble-or-tunnel-basic-wpf-events

您需要做的是标记鼠标事件。

private void ClickOnBox(object o, MouseButtonEventArgs args) {
        tb.Text += "a";
        //do something so that the "MouseLeftButtonDown" of the main window doesn't fire
        args.Handled = true;
    }

set handle 属性属于 mousebuttoneventargs 中的true。因为这是一个气泡事件,并且通过将其设置为true,它将无法在父母中处理。

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
namespace testit
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            Grid mainGrid = (Grid)FindName("MainGrid");
            mainGrid.MouseLeftButtonDown += ClickInNowhere;
            TextBlock tb = new TextBlock
            {
                Text = "hellonworld"
            };
            tb.MaxWidth = 100;
            tb.MaxHeight = 100;
            mainGrid.Children.Add(tb);
            tb.MouseLeftButtonDown += ClickOnBox;
        }
        private void ClickInNowhere(object o, MouseButtonEventArgs args)
        {
            tb.Text = "hellonworld";
        }
        private void ClickOnBox(object o, MouseButtonEventArgs args)
        {
            tb.Text += "a";
            args.Handled = true;
        }
    }
}

最新更新