WPF XAML Button Click handler in InlineUIContainer problem -
i've got flowdocument has elements so:
<inlineuicontainer> <button click="button_click" tag="123456789890"> <image source="images\image1.png" /> </button> </inlineuicontainer>
this stored off in .xaml file , gets loaded @ point doing so:
flowdocument = xamlreader.load(xamlfile, parsercontext) flowdocument; flowdocumentreader.document = flowdocument;
the loading fails following error:
xamlparseexception - failed create 'click' text 'button_click'
the button_click method 1 exists within mainwindow in flowdocumentreader resides , idea tag of button has identifier (inventory id) , click handler inventory id.
if flowdocument in mainwindow.xaml, everything's fine button_click event handler suspect when loads file disk, knows nothing handler.
how can resolve this? ideas?
update
while think pavlo's solution work, ended doing following , seems work rather well. in flowdocumentreader xaml added following:
<flowdocumentreader buttonbase.click="button_click">
and removed click event xaml buttons. i'm still grappling wpf , xaml common click handler works, believe, because of routed events. when click happens of buttons in loaded flowdocument, bubbles until finds handler, in case 1 specified in flowdocumentreader element.
despite frustration had not understanding, neat works way.
update 2:
the side effect of relying on routed events handle click event flowdocument's buttons buttons part of flowdocumentreader end bubbling click events catch-all handler i've created, not want happen.
to solve this, relying upon fact in handler, looks so:
private void button_click(object sender, routedeventargs e) { if (e.source button) { messagebox.show("button in doc clicked"); } }
the "source" member in routedeventargs "button" buttons in flowdocument , "flowdocumentreader" ones part of flowdocumentreader. appears work though i'd interested in hearing other ideas.
you can try following. give name button , after have loaded flowdocument
use findname
retrieve button , hook click
handler.
<inlineuicontainer> <button x:name="mybutton" tag="123456789890"> <image source="images\image1.png" /> </button> </inlineuicontainer>
-
flowdocument = xamlreader.load(xamlfile, parsercontext) flowdocument; flowdocumentreader.document = flowdocument; button mybutton = (button)flowdocument.findname("mybutton"); mybutton.click = button_click;
if button isn't unique , cannot give name consider finding object of type button
in document have tag
property set id.
Comments
Post a Comment