WPF:如何处理位于单独文件中的 DataTemplate 中的事件?

发布于 2024-10-11 18:18:19 字数 2438 浏览 4 评论 0原文

我有一个带有 ListView 控件的 DataTemplate。此 DataTemplate 位于 Templates.xaml(它是一个 ResourceDictionary)中。然后,Template.xaml 通过 ResourceDictionary.MergedDictionaries 包含到我的主 UserControl SourceManager.xaml 中。我想引发 DataTemplate 的 ListView 的 SelectionChanged 事件,但我希望后面的代码中的处理程序位于 SourceManager.xaml.cs 中。

我怎样才能做到这一点?

模板.xaml:

<ResourceDictionary x:Class="LawBib.Templates"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

<DataTemplate x:Key="SectionTemplate">
    <StackPanel>
        <TextBlock Text="{Binding XPath=@Title}" />
        <ListView x:Name="GroupList" ItemsSource="{Binding XPath=Source}">
            <ListView.Template>
                <ControlTemplate>
                    <WrapPanel IsItemsHost="True">

                    </WrapPanel>
                </ControlTemplate>
            </ListView.Template>
            <ListView.ItemTemplate>
                <DataTemplate>
                    <StackPanel>
                        <Image Source="images/source.png" />
                        <TextBlock Text="{Binding XPath=@Title}" HorizontalAlignment="Center" />
                    </StackPanel>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </StackPanel>
</DataTemplate>

SourceManager.xaml:

<UserControl x:Class="LawBib.SourceManager"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300" Background="#f5f7f8">
    <UserControl.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Resources.xaml" />
                <ResourceDictionary Source="Templates.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </UserControl.Resources>
...
</UserControl>

I have a DataTemplate with a ListView control. This DataTemplate is located in Templates.xaml (which is a ResourceDictionary). Template.xaml is then included into my main UserControl SourceManager.xaml through ResourceDictionary.MergedDictionaries. I want to raise the SelectionChanged event of the DataTemplate's ListView but I want the handler in the code behind to be in SourceManager.xaml.cs.

How can I achieve that?

Templates.xaml:

<ResourceDictionary x:Class="LawBib.Templates"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

<DataTemplate x:Key="SectionTemplate">
    <StackPanel>
        <TextBlock Text="{Binding XPath=@Title}" />
        <ListView x:Name="GroupList" ItemsSource="{Binding XPath=Source}">
            <ListView.Template>
                <ControlTemplate>
                    <WrapPanel IsItemsHost="True">

                    </WrapPanel>
                </ControlTemplate>
            </ListView.Template>
            <ListView.ItemTemplate>
                <DataTemplate>
                    <StackPanel>
                        <Image Source="images/source.png" />
                        <TextBlock Text="{Binding XPath=@Title}" HorizontalAlignment="Center" />
                    </StackPanel>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </StackPanel>
</DataTemplate>

SourceManager.xaml:

<UserControl x:Class="LawBib.SourceManager"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300" Background="#f5f7f8">
    <UserControl.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Resources.xaml" />
                <ResourceDictionary Source="Templates.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </UserControl.Resources>
...
</UserControl>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

挽清梦 2024-10-18 18:18:19

由于 SelectionChanged 是一个 RoatedEvent,因此您可以将其应用到您的 UserControl,如下所示:

<UserControl ...
             ListView.SelectionChanged="MyEventHandler" />

请注意,将为 调用此事件处理程序所有属于 UserControl 后代的 Selector 派生类(因为 Selector 是定义和引发事件的地方),其中包括组合框菜单列表框等。

Since SelectionChanged is a RoutedEvent, you can apply it to your UserControl like so:

<UserControl ...
             ListView.SelectionChanged="MyEventHandler" />

Be aware that this event handler will be called for all Selector derived classes (as Selector is where the event is defined and raised) that are descendants of your UserControl, which includes ComboBox, Menu, ListBox, etc.

野心澎湃 2024-10-18 18:18:19

创建一个行为

并将其放入数据模板中。

就是这样。

Create a Behavior

drop it in the Data Template.

that's it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文