WPF 处理来自 UserControl 的 RoutedEvent
我有一个UserControl,默认由VS生成,只添加了TextBlock:
<UserControl x:Class="MyNameSpace.Presentation.MyControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Name="myControl">
<Grid>
<TextBlock x:Name="SomeTextBox" Text="SomeText"></TextBlock>
</Grid>
现在,我从后面的代码动态地将这个控件的几个实例放入父控件的 WrapPanel 中。 我想处理来自 MyControl 实例的所有鼠标左键单击。 我有以下代码:
<UserControl x:Class="Minimo.Presentation.FirstParent"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Presentation="clr-namespace:Minimo.Presentation"
Height="300" Width="300">
<WrapPanel Name="wrapPanelOfMyControls" MyControl.MouseLeftButtonDown="WrapPanel1_OnMouseLeftButtonDown">
</WrapPanel>
在事件处理程序中,我做了一些操作,并且它起作用了。 但是,在编辑 XAML 文件时出现以下错误: 在类型“MyControl”中找不到可附加属性“MouseLeftButtonDown”。 如何解决这个问题?
I have a UserControl, default one generated by VS, only TextBlock is added:
<UserControl x:Class="MyNameSpace.Presentation.MyControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Name="myControl">
<Grid>
<TextBlock x:Name="SomeTextBox" Text="SomeText"></TextBlock>
</Grid>
Now, I put several instances of this control into parent control's WrapPanel dynamically from the code behind. I want to handle all Left Mouse Button clicks from MyControl instances. I have following code:
<UserControl x:Class="Minimo.Presentation.FirstParent"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Presentation="clr-namespace:Minimo.Presentation"
Height="300" Width="300">
<WrapPanel Name="wrapPanelOfMyControls" MyControl.MouseLeftButtonDown="WrapPanel1_OnMouseLeftButtonDown">
</WrapPanel>
In the event handler I make some action, and it works. However I get the following error when editing the XAML file:
The attachable property 'MouseLeftButtonDown' was not found in type 'MyControl'. How to fix this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这只是 XAML 编译器/设计器的一个错误,可以安全地忽略。 但是,您可以通过指定它更熟悉的类型来“修复”它:
This is just a bug with the XAML compiler/designer and can be safely ignored. However, you may be able to "fix" it by specifying a type it is more intimately aware of: