C# WPF按钮单击事件不工作
我有一个按钮,其中包含一个网格内的图像,我的问题是我无法使按钮C# WPF按钮单击事件不工作,c#,wpf,xaml,C#,Wpf,Xaml,我有一个按钮,其中包含一个网格内的图像,我的问题是我无法使按钮单击事件工作 我的XAML代码: .... <TabControl TabStripPlacement="Left"> <TabItem Loaded="ListProductsLoaded" Width="185" Height="100" Header="Lister les Produits"> <Grid Background="#FFE5E5E5">
单击事件工作
我的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>
....
如果以前有人遇到过这个问题,请帮我解决,我一直在寻找解决方案,但没有成功。提前谢谢
编辑:即使我使用一个普通的按钮:没有图像,它也不工作
解决方案:实际上,我将选项卡Control
的选项IshitteVisible
设置为false
,并禁用了所有点击事件…对不起大家。您的点击事件在codebehind中被命名为Test\u OnClick
,但在XAML中您将其称为点击=“图像按钮点击”
因此事件不会触发。它需要是Click=“Test\u OnClick”
您可以从XAML中删除所有事件订阅,并使用压缩语法(仅一行代码)将其放入代码中,如下所示:
ImageButton.Click += (s, e) => { MessageBox.Show("test button clicked"); };
另外,删除事件处理过程:
private void ImageButtonClicked(object sender, RoutedEventArgs e)
{
MessageBox.Show(this, "clicked image button");
}
除此之外,同一按钮的另一个事件处理程序(XAML中的PreviewMouseLeftButtonDown=“ImageButtonLeftMouseButtonDown”
)似乎是多余的–您可以使用相应的事件处理程序将其删除
因此,代码段应该如下所示:
MainWindow()
{
InitializeComponent();
ImageButton.Click += (s, e) => { MessageBox.Show("test button clicked"); };
// ... the rest of the code...
与您的案例相关(Button
control inListView
template),请参阅本文:尝试在不使用click事件的情况下使用Tap事件。两个事件中的功能相同
不,这不是我的确切xaml,我必须更改它。但是ImageButtonClicked方法存在,比如Test_OnClick.Alex有一个很好的解决方案,或者在后面的代码中看到这一点,将语句放在我的解决方案中public MainWindow()行的后面{InitializeComponent();当我想在代码隐藏中添加事件时,我无法访问该按钮。在窗口构造函数中,甚至在事件方法中:你给我的解决方案中没有一个有效,因为我无法访问ImageButton属性。阅读修改后的帖子,它解释了如何操作。Rgds,好的,我看到了问题:这不仅仅是一个按钮,它是Lis中的一个按钮tView模板。看看这个解决方案,它可能会有帮助:我知道它是click事件,我说的是Button控件。XAML:CS:testButton.click…无法解决symbol testButton我有一个类似的问题:Button\u click
-事件没有触发,因为我还有一个Button\u PreviewMouseUp
-事件,其中包含e.Handled=true
。我删除它后,单击事件生效。
MainWindow()
{
InitializeComponent();
ImageButton.Click += (s, e) => { MessageBox.Show("test button clicked"); };
// ... the rest of the code...