使用事件/命令与的XamlReader [英] Using Events/Commands with XamlReader

查看:220
本文介绍了使用事件/命令与的XamlReader的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我动态构建使用XamlReader.Parse(串),我的DataTemplate。我的问题是,我不能把任何事件上的任何控件创建使用的XamlReader的。做一些研究网上后,我了解到,这是的XamlReader的一个已知的限制。

我不知道了很多关于在WPF的命令,但我能以某种方式利用它们来获得同样的结果?如果是的话怎么样?如果不是有没有办法使用XAML阅读器?

创建我能处理的事件在我的code从背后控制

下面是我创建DataTemplate中的一个例子。我在将承载这个DataTemplate中窗口的codebehind定义的MenuItem_Click事件处理程序。

我试图运行它时,收到以下错误:System.Windows.Markup.XamlParseException是未处理:无法创建一个点击从文本MenuItem_Click

 的DataTemplate结果= NULL;
        StringBuilder的SB =新的StringBuilder();

        sb.Append(@<的DataTemplate
                        的xmlns =HTTP://schemas.microsoft.com/winfx/2006/xaml/$p$psentation
                        的xmlns:X =HTTP://schemas.microsoft.com/winfx/2006/xaml'>
                            <电网WIDTH =自动高度=自动>

                            < TextBlock的文本=你好>
                                < TextBlock.ContextMenu>
                                    <文本菜单>
                                         <菜单项
                                          标题=世界
                                          点击=MenuItem_Click>< /菜单项>
                                    < /文本菜单>
                                < /TextBlock.ContextMenu>
                            < / TextBlock的>

                            < /网格>
                      &所述; / DataTemplate中>中);

        结果= XamlReader.Parse(sb.ToString())为DataTemplate中;
 

解决方案

希望能晚答案可能帮助他人:

我发现我需要为绑定的事件后,解析,然后不得不从XAML中字符串中删除的单击事件。

在我的情况,我申请所产生的DataTemplate到一个ItemTemplate,接线的ItemSource,然后添加了处理程序。这也意味着 click事件将是相同的所有项目,但在我的情况下头球攻门被需要的信息和方法是一样的。

  //设置DataTemplate中的XAML解析的结果。
myListView.ItemTemplate =(DataTemplate中)的结果;

//添加ItemTemplate中第一个,否则会出现一个视觉的孩子错误
myListView.ItemsSource = this.ItemsSource;

//附加Click事件。
myListView.AddHandler(MenuItem.ClickEvent,新RoutedEventHandler(MenuItem_Click));
 

然后点击事件需要找回到原始出处,发件人将在我的情况下被使用的DataTemplate ListView控件。

 内部空隙MenuItem_Click(对象发件人,RoutedEventArgs E){
    菜单项MI = e.OriginalSource的菜单项;
    //在这一点上,可以根据需要访问该菜单项的标题或其他信息。
}
 

I am dynamically building my datatemplate using XamlReader.Parse(string). The problem I have is that I can't put any events on any of the controls I create using the XamlReader. After doing some research online I've learned that this is a known limitation of XamlReader.

I don't know a lot about commands in WPF but could I somehow use them to gain the same result? If so how? If not is there any way I can handle an event in my code behind from a control created using Xaml Reader?

Below is an example of the datatemplate I create. I have the MenuItem_Click event handler defined in the the codebehind of the Window that will host this datatemplate.

I get the following error when trying to run it: System.Windows.Markup.XamlParseException was unhandled: Failed 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;

解决方案

Hoping a late answer might help others:

I found that I needed to bind the events after the parsing, and had to remove the click event from the Xaml string.

In my scenario I applied the resulting DataTemplate to an ItemTemplate, wired up the ItemSource, and then added the handler. This does mean the click event would be the same for all the items, but in my case the header was the information needed and the method was the same.

//Set the datatemplate to the result of the xaml parsing.
myListView.ItemTemplate = (DataTemplate)result;

//Add the itemtemplate first, otherwise there will be a visual child error
myListView.ItemsSource = this.ItemsSource; 

//Attach click event.
myListView.AddHandler(MenuItem.ClickEvent, new RoutedEventHandler(MenuItem_Click));

And then the click event needs to get back to the original source, the sender will be the ListView in my case that was using the DataTemplate.

internal void MenuItem_Click(object sender, RoutedEventArgs e){
    MenuItem mi = e.OriginalSource as MenuItem;
    //At this point you can access the menuitem's header or other information as needed.
}

这篇关于使用事件/命令与的XamlReader的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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