Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# PreviewMouseLeftButtonUp未按预期触发_C#_Wpf_Events - Fatal编程技术网

C# PreviewMouseLeftButtonUp未按预期触发

C# PreviewMouseLeftButtonUp未按预期触发,c#,wpf,events,C#,Wpf,Events,我有以下XAML: <UserControl x:Class="WPFSandpit.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Height="350" Width="525"> <Border BorderThickness="1"

我有以下XAML:

<UserControl x:Class="WPFSandpit.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Height="350" Width="525">
<Border BorderThickness="1" BorderBrush="Blue" Width="100" Height="35" PreviewMouseLeftButtonUp="OnPreviewMouseLeftButtonUp">
    <Grid >
        <Grid.Style>
            <Style>
                <Style.Triggers>
                    <Trigger Property="Border.IsMouseOver" Value="True">
                        <Setter Property="Border.Background" Value="Red" />
                    </Trigger>
                </Style.Triggers>
            </Style>
        </Grid.Style>
        <TextBlock Text="MyText"  Margin="2" VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="14"
                       FontWeight="SemiBold"/>
    </Grid>
</Border>

我很难理解为什么只在文本块中触发事件,而在边框内使用鼠标时不触发事件?

乍一看,文本框似乎正在处理事件,只是没有弹出事件,因为文本框将处理所有鼠标事件,如预览、捕获、拖动、,等等。如果在XAML中没有文本框,那么这可能就是正在发生的事情


不确定最终目标是基于您的代码,但无论您计划在border事件处理程序中为该事件执行什么操作,都需要在TextBox事件处理程序中处理。或者,您可以在TextBox(创建事件处理程序)中处理事件,调用基本处理程序,然后将Handled设置为false。

我假设,您希望文本框在边界上方时变为红色,而不是在边界内部时变为红色(如果你真的想触发OnPreviewMouseLeftButtonUp,你必须把你的问题说得更清楚,对我来说,它似乎可以触发你的代码)。基于此假设,您只是将样式放置在错误的范围内。将样式应用于xaml中的边框节点时,其行为与下面的代码段所示的预期一致。请注意,我增加了边框宽度,以便更容易触发事件和颜色更改

<Window x:Class="WpfApplication1.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:WpfApplication1"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Border BorderThickness="10" BorderBrush="Blue" Width="100" Height="35" PreviewMouseLeftButtonUp="OnPreviewMouseLeftButtonUp">
        <Border.Style>
            <Style>
                <Style.Triggers>
                    <Trigger Property="Border.IsMouseOver" Value="True">
                        <Setter Property="Border.Background" Value="Red" />
                    </Trigger>
                </Style.Triggers>
            </Style>
        </Border.Style>
        <Grid >
                <TextBlock Text="MyText"  Margin="2" VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="14"
                       FontWeight="SemiBold"/>
            </Grid>
        </Border>
</Window>

c#代码:

命名空间WpfApplication1
{
/// 
///MainWindow.xaml的交互逻辑
/// 
公共部分类主窗口:窗口
{
公共主窗口()
{
初始化组件();
}
私有void OnPreviewMouseLeftButtonUp(对象发送器,鼠标按钮Ventargs e)
{
MessageBox.Show(“所选”+sender.ToString());
}
}
}

你确定你在点击
边框
本身吗?毕竟它只有1像素厚,所以很难点击它。如果你用
触发器
作为它在
边框
内的指示,那么我想那里也有问题。你应该像J.G.所说的那样在
边框
中设置它。如果要在
网格中设置
,请尝试为
边框指定
名称,然后使用
<Window x:Class="WpfApplication1.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:WpfApplication1"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Border BorderThickness="10" BorderBrush="Blue" Width="100" Height="35" PreviewMouseLeftButtonUp="OnPreviewMouseLeftButtonUp">
        <Border.Style>
            <Style>
                <Style.Triggers>
                    <Trigger Property="Border.IsMouseOver" Value="True">
                        <Setter Property="Border.Background" Value="Red" />
                    </Trigger>
                </Style.Triggers>
            </Style>
        </Border.Style>
        <Grid >
                <TextBlock Text="MyText"  Margin="2" VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="14"
                       FontWeight="SemiBold"/>
            </Grid>
        </Border>
</Window>
namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void OnPreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            MessageBox.Show("Selected" + sender.ToString());
        }
    }
}