带有点击事件的 XamlReader
我在我的 WPF 项目中使用 XamlReader。它有效(我的参考)
我当前的示例Xaml是这样的:
<Grid xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" Width="800" Height="600">
<Button Name="Test1" Content="Test1" Width="357" Height="88" Margin="14,417,0,0" ></Button>
<Button Name="Test2" Content="Test2" Width="357" Height="88" Margin="14,529,0,0" ></Button>
</Grid>
并添加按钮的单击事件,如下所示:
button = LogicalTreeHelper.FindLogicalNode(rootObject, "Test1") as Button ;
button.Click += new RoutedEventHandler(Button1_Click);
是否可以这样编写xaml?
<Grid xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" Width="800" Height="600">
<Button Name="Test1" Content="Test1" ... Click="Button1_Click"></Button>
<Button Name="Test2" Content="Test2" ... Click="Button2_Click"></Button>
</Grid>
I am using XamlReader in my WPF project. And it works (My reference)
My current sample Xaml is like that:
<Grid xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" Width="800" Height="600">
<Button Name="Test1" Content="Test1" Width="357" Height="88" Margin="14,417,0,0" ></Button>
<Button Name="Test2" Content="Test2" Width="357" Height="88" Margin="14,529,0,0" ></Button>
</Grid>
and adding button's click event like this:
button = LogicalTreeHelper.FindLogicalNode(rootObject, "Test1") as Button ;
button.Click += new RoutedEventHandler(Button1_Click);
Is it possible to write xaml like this?
<Grid xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" Width="800" Height="600">
<Button Name="Test1" Content="Test1" ... Click="Button1_Click"></Button>
<Button Name="Test2" Content="Test2" ... Click="Button2_Click"></Button>
</Grid>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不允许
XamlReader.Load
在其中附加eventHandlers
。因此,请使用此技术将eventHandlers
动态附加到它。1- 编写不带
eventHandlers
的 Xaml 字符串 - 但编写这些控件的 Name 属性。2- 使用
XamlReader.Load(str);
加载字符串3- 然后从中加载 DataTemplate 的内容。 using
Grid template = ((Grid)(dt.LoadContent()));
注意:这里
Grid
是DataTemplate
中的父控件。4- 按名称找到要附加事件处理程序的控件。
Button img = (Button)template.FindName("MyButtonInDataTemplate");
我希望它有帮助。
XamlReader.Load
not allowed to attacheventHandlers
in it. so use this technique to dynamically attach theeventHandlers
to it.1- Write your Xaml string without
eventHandlers
-But write the Name property of those Controls.2- Load the string with
XamlReader.Load(str);
3- Then load the content of DataTemplate from it. using
Grid template = ((Grid)(dt.LoadContent()));
Note: here
Grid
is the parent Control inDataTemplate
.4- Find the Control by Name you want to attach the Event Handler.
Button img = (Button)template.FindName("MyButtonInDataTemplate");
I hope it helps.
不可以。您无法使用原始 XAML 在运行时保存事件或加载事件。这是 XAML 序列化的限制,因为序列化的 XAML 是独立的,这意味着每个资源都应该在原始 XAML 中加载,而事件的代码逻辑则不然。点击此处了解更多信息
No. You can't save events or load them at run-time using raw XAML. It's a limitation of XAML Serialization, as serialized XAML is self-contained, means every resource should be in raw XAML to load and event's code logic is not . Read more here