如何添加事件处理程序来控制资源字典中的数据模板
我有一个资源字典:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="wpfUI2.MainWindowEvents">
<DataTemplate
x:Key="WorkspacesTemplate">
<TabControl
x:Name="Tab1"
IsSynchronizedWithCurrentItem="True"
ItemsSource="{Binding}"
ItemTemplate="{StaticResource ClosableTabItemTemplate}"
Margin="4"/>
</DataTemplate>
...
我想向 TabControl 添加一个事件处理程序。 MainWindowEvents 是在没有其他类的文件中定义的类:
Namespace wpfUI2
Public Class MainWindowEvents
End Class
End Namespace
当我去添加类似事件处理程序
<TabControl
x:Name="Tab1"
IsSynchronizedWithCurrentItem="True"
ItemsSource="{Binding}"
ItemTemplate="{StaticResource ClosableTabItemTemplate}"
Margin="4"
SelectionChanged=""
/>
并尝试在“”之间单击以创建事件时,我收到一条错误消息,指出 x:Class 属性指定的类必须成为文件中的第一个。嗯,确实如此!奇怪的是,当我手动创建处理程序时:
Namespace wpfUI2
Public Class MainWindowEvents
Public Sub Tab1_SelectionChanged(sender As System.Object, e As System.Windows.Controls.SelectionChangedEventArgs)
End Sub
End Class
End Namespace
一切都编译正常,但我在 window.show 上遇到运行时异常
我做错了什么?
I have a resource dictionary:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="wpfUI2.MainWindowEvents">
<DataTemplate
x:Key="WorkspacesTemplate">
<TabControl
x:Name="Tab1"
IsSynchronizedWithCurrentItem="True"
ItemsSource="{Binding}"
ItemTemplate="{StaticResource ClosableTabItemTemplate}"
Margin="4"/>
</DataTemplate>
...
And I want to add an event handler to the TabControl. MainWindowEvents is a class defined in a file with no other classes:
Namespace wpfUI2
Public Class MainWindowEvents
End Class
End Namespace
When I go to add an event handler like
<TabControl
x:Name="Tab1"
IsSynchronizedWithCurrentItem="True"
ItemsSource="{Binding}"
ItemTemplate="{StaticResource ClosableTabItemTemplate}"
Margin="4"
SelectionChanged=""
/>
and try to click between the "" to create the event I get an error saying that the class specified by the x:Class attribute must be the first in the file. Well it is!. Strangely, when I create the handler manually:
Namespace wpfUI2
Public Class MainWindowEvents
Public Sub Tab1_SelectionChanged(sender As System.Object, e As System.Windows.Controls.SelectionChangedEventArgs)
End Sub
End Class
End Namespace
Everything compiles ok, but i get a runtime exception on window.show
What am i doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于以下原因,我能够使其工作:
是否可以在 WPF 中的资源字典后面设置代码以进行事件处理?
我发现您的代码中缺少一些内容,与那里的示例进行比较。
I was able to make it work thanks to this:
Is it possible to set code behind a resource dictionary in WPF for event handling?
I see missing stuff in your code, compare to the sample there.