wpf WPF按钮单击事件不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25110042/
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 12:15:59  来源:igfitidea点击:

WPF Button Click Event not working

c#wpfxaml

提问by Redaa

I have a button that contains an Image inside a grid, my problem is that I can't make the button Clickevent work.

我有一个在网格中包含图像的按钮,我的问题是我无法使按钮Click事件工作。

My XAML code:

我的 XAML 代码:

....
<TabControl TabStripPlacement="Left">
    <TabItem Loaded="ListProductsLoaded" Width="185" Height="100" Header="Lister les Produits">
        <Grid Background="#FFE5E5E5">
            <ListView Name="ProductsListView" IsHitTestVisible="False">
                <ListView.View>
                    <GridView>
                        <GridViewColumn Width="120" Header="Reference" DisplayMemberBinding="{Binding ProductReference}"></GridViewColumn>
                        <GridViewColumn Width="150" Header="Nom" DisplayMemberBinding="{Binding ProductName}"></GridViewColumn>
                        <GridViewColumn Width="120" Header="Photo" >
                            <GridViewColumn.CellTemplate>
                                <DataTemplate>
                                    <Button PreviewMouseLeftButtonDown="ImageButtonLeftMouseButtonDown" Name="ImageButton" Width="120" Height="120" Click="ImageButtonClicked">
                                        <Image Width="119" Height="119" Name="ProdImage" Source="{Binding ProductImage}"></Image>
                                    </Button>
                                </DataTemplate>
                            </GridViewColumn.CellTemplate>
                        </GridViewColumn>
                    </GridView>
                </ListView.View>
            </ListView>
        </Grid>
    </TabItem>
</TabControl>
....

As you can see I used two events, PreviewMouseLeftButtonDownevent and Click event, I used PreviewMouseLeftButtonDowndue to a solution I saw in a stackoverflow question but it didn't work. Both events methods display a MessageBox

如您所见,我使用了两个事件,PreviewMouseLeftButtonDown事件和单击事件,PreviewMouseLeftButtonDown由于我在 stackoverflow 问题中看到的一个解决方案,我使用了它,但它不起作用。两种事件方法都显示一个MessageBox

private void ImageButtonClicked(object sender, RoutedEventArgs e)
{
    MessageBox.Show(this, "clicked image button");
}

private void ImageButtonLeftMouseButtonDown(object sender, MouseButtonEventArgs e)
{
    MessageBox.Show("test clicked");
}

If anyone encountered this problem before please help me solve it, I searched for a solution without success. Thanks in advance.

如果之前有人遇到过这个问题,请帮我解决,我搜索了一个没有成功的解决方案。提前致谢。

Edit: Even if I use a normal button: No image in it, it doesn't work.

编辑:即使我使用普通按钮:其中没有图像,它也不起作用。

Solution : Actually, i set the Option IsHitTestVisibleof the TabControlto falseand that disabled all Clickevents… sorry everybody.

解决方法:其实,我的选项设置IsHitTestVisibleTabControl,以false和禁用所有Click事件...对不起大家。

回答by Alexander Bell

You can remove all event subscription from XAML and put it in code behind using compact syntax (just a single line of code) as shown below:

您可以从 XAML 中删除所有事件订阅,并使用紧凑的语法(仅一行代码)将其放入代码中,如下所示:

ImageButton.Click += (s, e) => { MessageBox.Show("test button clicked"); };

Also, REMOVEyour event handling proc:

另外,删除您的事件处理过程:

private void ImageButtonClicked(object sender, RoutedEventArgs e)
{
    MessageBox.Show(this, "clicked image button");
}

In addition to this, another event handler for the same button (PreviewMouseLeftButtonDown="ImageButtonLeftMouseButtonDown"in XAML) seems redundant – you can remove it with corresponding event handling proc.

除此之外,同一个按钮的另一个事件处理程序(PreviewMouseLeftButtonDown="ImageButtonLeftMouseButtonDown"在 XAML 中)似乎是多余的——您可以使用相应的事件处理程序删除它。

So, the code snippet should look like:

因此,代码片段应如下所示:

MainWindow()
{
    InitializeComponent();
    ImageButton.Click += (s, e) => { MessageBox.Show("test button clicked"); };

// ... the rest of the code...

Pertinent to your case (Buttoncontrol in ListViewtemplate), refer to this article: WPF ListView with buttons on each line

与您的情况相关(模板中的Button控件ListView),请参阅这篇文章:WPF ListView with button on each line

回答by pathDongle

Your Click event is named Test_OnClickin codebehind but in XAML you refer to it as Click="ImageButtonClicked"so the event does not fire. It needs to be Click="Test_OnClick"

您的 Click 事件Test_OnClick在代码隐藏中命名,但在 XAML 中您引用它,Click="ImageButtonClicked"因此该事件不会触发。它需要是Click="Test_OnClick"

回答by Sanjaya

try using Tap event without using click event.Same function in both events,

尝试使用点击事件而不使用点击事件。两个事件中的功能相同,