Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在XamlReader中使用事件/命令_C#_.net_Wpf_Xaml_Xamlreader - Fatal编程技术网

C# 在XamlReader中使用事件/命令

C# 在XamlReader中使用事件/命令,c#,.net,wpf,xaml,xamlreader,C#,.net,Wpf,Xaml,Xamlreader,我正在使用XamlReader.Parse(字符串)动态构建我的数据模板。我的问题是,我不能在使用XamlReader创建的任何控件上放置任何事件。在网上做了一些研究之后,我了解到这是XamlReader的一个已知限制 我对WPF中的命令知之甚少,但我是否可以使用它们来获得相同的结果?如果是,怎么做?如果没有,是否有任何方法可以从使用Xaml Reader创建的控件处理代码隐藏中的事件 下面是我创建的datatemplate的一个示例。我在将承载此数据模板的窗口的代码隐藏中定义了MenuItem

我正在使用XamlReader.Parse(字符串)动态构建我的数据模板。我的问题是,我不能在使用XamlReader创建的任何控件上放置任何事件。在网上做了一些研究之后,我了解到这是XamlReader的一个已知限制

我对WPF中的命令知之甚少,但我是否可以使用它们来获得相同的结果?如果是,怎么做?如果没有,是否有任何方法可以从使用Xaml Reader创建的控件处理代码隐藏中的事件

下面是我创建的datatemplate的一个示例。我在将承载此数据模板的窗口的代码隐藏中定义了MenuItem\u Click事件处理程序

尝试运行时出现以下错误:System.Windows.Markup.XamlParseException未处理:未能从文本“菜单项\单击”创建“单击”

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;
DataTemplate结果=null;
StringBuilder sb=新的StringBuilder();
某人附加(@)
");
result=XamlReader.Parse(sb.ToString())作为数据模板;
看一看。那里的大多数解决方案也适用于
Parse
。我不是一个真正的C#dev,所以我唯一能真正解释的是最后一个,这是一个“如果其他一切都失败了”的选项:

首先,将ID添加到XAML中,而不是单击等属性。然后您可以使用获取节点,然后自己连接事件

例如,假设您提供了MenuItem
ID=“WorldMenuItem”
。然后在调用parse后的代码中,可以执行以下操作:

MenuItem worldMenuItem = (MenuItem)LogicalTreeHelper.FindLogicalNode(result, "WorldMenuItem");
worldMenuItem.Click += MenuItem_Click; // whatever your handler is

希望迟来的回答能帮助他人:

我发现我需要在解析后绑定事件,并且必须从Xaml字符串中删除click事件

在我的场景中,我将结果DataTemplate应用于ItemTemplate,连接ItemSource,然后添加处理程序。这确实意味着所有项目的点击事件都是相同的,但在我的例子中,标题是所需的信息,方法是相同的

//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));
然后单击事件需要返回到原始源,发送者将是使用DataTemplate的ListView

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.
}

我似乎无法编译那个片段。FindLogicNode将DependencyObject作为第一个参数,我不知道如何将DataTemplate强制转换为DependencyObject。有什么想法吗?我想我知道了如何从DataTemplate中获取DependencyObject…我使用DataTemplate.LoadContent()。现在的问题是,无论菜单项是什么,都找不到。我知道上下文菜单不包括在与其他控件相同的VisualTree中,LogicalTree也是这样吗?