WPF XAML Button Click handler in InlineUIContainer

2019-07-18 09:04发布

问题:

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>

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 - Failed to create a 'Click' from the text 'Button_Click'

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.

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.

How can I resolve this? Ideas?


Update

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">

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.


Update 2:

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");
   }
}

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.

回答1:

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;

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.



回答2:

If you create a custom button class (derive from button) and add your own custom click event definition. You could then just add a handler that is specific to your custom event. No need to worry about picking up unintended events from other buttons.

Declare the new event in your button class

 Public Shared ReadOnly ButtonClickEvent As RoutedEvent = _
               EventManager.RegisterRoutedEvent("ButtonClick", RoutingStrategy.Bubble, _
               GetType(RoutedEventHandler), GetType(MyButton))

' Provide CLR accessors for the event
Public Custom Event ButtonClick As RoutedEventHandler
    AddHandler(ByVal value As RoutedEventHandler)
        Me.AddHandler(ButtonClickEvent, value)
    End AddHandler

    RemoveHandler(ByVal value As RoutedEventHandler)
        Me.RemoveHandler(ButtonClickEvent, value)
    End RemoveHandler

    RaiseEvent(ByVal sender As Object, ByVal e As RoutedEventArgs)
        Me.RaiseEvent(e)
    End RaiseEvent
End Event

In the click handler of your custom button class raise the new RoutedEvent

    Private Sub MyButton_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
        RaiseEvent ButtonClick(Me, New RoutedEventArgs(MyButton.ButtonClickEvent))
    End Sub

The listen for that with an event handler at the FlowDocumentReader or other top-level element. (I have a GroupBox around my flow document viewer and have added the handler at that level)

 <GroupBox Header="Document editor" local:MyButton.ButtonClick="OnButtonClick" >