我有一个资源字典:
<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上出现运行时异常
我究竟做错了什么?
最佳答案
由于以下原因,我得以使其工作:
Is it possible to set code behind a resource dictionary in WPF for event handling?
我发现您的代码中缺少内容,请与其中的示例进行比较。
关于wpf - 如何在资源字典的datatemplate中添加事件处理程序来控制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7330043/