WPF XAML按钮单击InlineUIContainer问题中的处理程序 [英] WPF XAML Button Click handler in InlineUIContainer problem

查看:374
本文介绍了WPF XAML按钮单击InlineUIContainer问题中的处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个FlowDocument,它有一些元素,如下所示:

I've got a FlowDocument which has some elements like so:

<InlineUIContainer>
   <Button Click="Button_Click" Tag="123456789890">
      <Image Source="Images\Image1.png" />
   </Button>
</InlineUIContainer>

这是存储在.xaml文件中,并通过执行以下操作在某些时候加载:

This is stored off in a .xaml file and gets loaded at some point by doing something like so:

flowDocument = XamlReader.Load(xamlFile, parserContext) as FlowDocument;
flowDocumentReader.Document = flowDocument;

加载失败,出现以下错误:

The loading fails with the following error:

XamlParseException - 无法从文本Button_Click中创建点击

Button_Click方法是MainWindow中存在的方法FlowDocumentReader所在的位置,其想法是按钮的标签具有一些标识符(清单ID),点击处理程序将使用该清单ID进行操作。

The Button_Click method is one which exists within the MainWindow in which the FlowDocumentReader resides and the idea is that the Tag of the button has some identifier (inventory id) and the click handler will do something with that inventory id.

如果FlowDocument在MainWindow.xaml中,这个Button_Click事件处理程序的一切都很好,但我怀疑当从磁盘加载文件时,它对处理程序一无所知。

If the FlowDocument is in the MainWindow.xaml, everything's fine with this Button_Click event handler but I suspect that when it loads the file from disk, it knows nothing about the handler.

我该如何解决?想法?

更新

虽然我认为Pavlo的解决方案可以起作用,但我最终做了以下工作,似乎工作得很好。在我的FlowDocumentReader Xaml中,我添加了以下内容:

While I think Pavlo's solution would work, I ended up doing the following and it seems to work rather well. In my FlowDocumentReader Xaml I added the following:

<FlowDocumentReader ButtonBase.Click="Button_Click">

,并从xaml中删除了按钮的点击事件。我仍然在努力与WPF和XAML,但这个常见的点击处理程序工作,我相信,因为路由事件。当点击发生在我加载的FlowDocument中的任何按钮时,它会起起来,直到找到一个处理程序,在我的例子中是在FlowDocumentReader元素中指定的。

and removed the click event from the xaml for the buttons. I'm still grappling with WPF and XAML but this common click handler works, I believe, because of routed events. When the Click happens for any of the buttons in my loaded FlowDocument, it bubbles up until it finds a handler, in my case the one specified in the FlowDocumentReader element.

尽管我不了解的沮丧,这样做很整洁。

Despite the frustration I had from not understanding, it is neat that it works this way.

更新2: / strong>

Update 2:

依赖路由事件来处理我的FlowDocument的按钮的Click事件的副作用是,作为FlowDocumentReader本身的一部分的按钮最终冒泡点击事件进入我创建的这个全部处理程序,这绝对不是我想要发生的。

The side effect of relying on routed events to handle the Click event for my FlowDocument's buttons is that the buttons which are part of the FlowDocumentReader itself end up bubbling their Click events into this catch-all handler I've created, which is definitely not what I want to happen.

为了解决这个问题,我目前依靠的事实在处理程序中,如下所示:

To solve this, I am currently relying upon the fact that in the handler, which looks like so:

private void Button_Click(object sender, RoutedEventArgs e)
{
   if (e.Source is Button)
   {
      MessageBox.Show("Button in doc clicked");
   }
}

RoutedEventArgs中的Source成员是Button 用于FlowDocument中的按钮,FlowDocumentReader用于FlowDocumentReader的一部分。出现工作,虽然我有兴趣听到其他想法。

the "Source" member in the RoutedEventArgs is "Button" for the buttons in the FlowDocument and "FlowDocumentReader" for the ones that are part of the FlowDocumentReader. Appears to work though I'd be interested in hearing other ideas.

推荐答案

你可以尝试以下。给你的按钮一个名字,加载 FlowDocument 使用 FindName 来检索按钮并挂接点击处理程序。

You can try the following. Give a name to your button and after you have loaded the FlowDocument use FindName to retrieve the button and hook up the Click handler.

<InlineUIContainer>
   <Button x:Name="MyButton" Tag="123456789890">
      <Image Source="Images\Image1.png" />
   </Button>
</InlineUIContainer>

-

flowDocument = XamlReader.Load(xamlFile, parserContext) as FlowDocument;
flowDocumentReader.Document = flowDocument;

Button myButton = (Button)flowDocument.FindName("MyButton");
myButton.Click = Button_Click;

如果按钮不是唯一的,您不能给它一个名称, code>按钮在具有设置为ID的标签属性的文档中。

If you button isn't unique and you cannot give it a name consider finding all object of type Button in the document that have the Tag property set to an ID.

这篇关于WPF XAML按钮单击InlineUIContainer问题中的处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆