102425074
102425074

Reputation: 811

How can I make the control can not be click while overlying?

I make an easy demo by WPF, here is the XAML:

<Window x:Class="WpfApp1.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:WpfApp1"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid PreviewMouseLeftButtonDown="Grid_PreviewMouseLeftButtonDown" Background="White">
        <Border Height="100" Width="100" Background="Red" PreviewMouseLeftButtonDown="Border_PreviewMouseLeftButtonDown"></Border>
    </Grid>
</Window>

And here is the code-behind:

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;

namespace WpfApp1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }


        private void Border_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            MessageBox.Show("Border");
        }

        private void Grid_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            MessageBox.Show("Grid");
        }
    }
}

I wanna do that if I press the border, it only activates the "Border" message box but not with the "Grid" message box also.

How can I do it? Would you please help me?

Thank you.

Upvotes: 0

Views: 41

Answers (1)

juster zhu
juster zhu

Reputation: 96

you can replace PreviewMouseLeftButtonDown with MouseDown.

Upvotes: 2

Related Questions