I am dynamically creating my data file using XamlReader.Parse (string). The problem is that I cannot put any events in any controls that I create using XamlReader. After doing some research on the Internet, I found out that this is a known limitation of XamlReader.
I donβt know much about commands in WPF, but can I somehow use them to get the same result? If so, how? If not, can I handle the event in my code behind the control created with Xaml Reader?
The following is an example of a data file created. I have a MenuItem_Click event handler defined in the window code that will host this data file.
When I try to start it, the following error occurs: System.Windows.Markup.XamlParseException was unhandled: it was not possible to create a 'Click' from the text 'MenuItem_Click'.
DataTemplate result = null;
StringBuilder sb = new StringBuilder();
sb.Append(@"<DataTemplate
xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'
xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
<Grid Width=""Auto"" Height=""Auto"">
<TextBlock Text=""Hello"">
<TextBlock.ContextMenu>
<ContextMenu>
<MenuItem
Header=""World""
Click=""MenuItem_Click""></MenuItem>
</ContextMenu>
</TextBlock.ContextMenu>
</TextBlock>
</Grid>
</DataTemplate>");
result = XamlReader.Parse(sb.ToString()) as DataTemplate;
source
share