wpf - 如何在资源字典的datatemplate中添加事件处理程序来控制

标签 wpf event-handling datatemplate resourcedictionary

我有一个资源字典:

<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/

相关文章:

c# - 泄漏 Web 服务完成事件?

javascript - 你如何处理 jQuery 中的 oncut、oncopy 和 onpaste?

c# - 大型可滚动数据 SL4 的虚拟化性能问题

angularjs - 在 AngularJS 中创建数据模板

WPF - UserControl 默认内容属性

c# - 如何在 WPF 4.0 中创建发光的文本框?

.net - XamlReader.Load找不到静态资源

wpf - 绑定(bind) SolidColorBrush 的颜色并使用转换器更改它

javascript - 多少个 JavaScript 事件处理程序太多了?

WPF TreeView : Using ItemTemplateSelector and ItemContainerStyle Not Working At Same Time