将不同的点击处理程序分配给全局控件模板 [英] assigning different click handlers to a global control template

查看:23
本文介绍了将不同的点击处理程序分配给全局控件模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是这个 SimpleExpander 的一个稍微修改过的版本,以便我的扩展器标题可以点击.

I am using a slightly modified version of this SimpleExpander so that my expander headers are clickable.

http://www.codeproject.com/Articles/248112/模板-WPF-扩展器-控制

这是相关的源代码:

<ControlTemplate x:Key="SimpleExpanderTemp" TargetType="{x:Type Expander}">

            <DockPanel>

                <StackPanel DockPanel.Dock="Top" Orientation="Horizontal">
                    <ToggleButton x:Name="ExpanderButton" />

                    <!-- button with hard coded click handler -->
                    <Button Name="SharerHeader" Click="SharerHeader_Click_1"> 
                        <ContentPresenter Content="{TemplateBinding Header}" />
                    </Button>
                </StackPanel>

            </DockPanel>
        </ControlTemplate>

效果很好.现在我想在我的应用程序中重用这个控件模板,所以我将代码移到了我的 app.xaml 中.但是,我的标题被硬编码为点击事件,我希望能够根据使用扩展器的页面设置点击处理程序.我该如何实现?

Works great. Now I want to reuse this control template across my application, so I moved the code into my app.xaml. However, my header is hardcoded to a click event, and I want to be able to set the click handler based on the page the expander is being used in. How can I accomplish this?

推荐答案

  1. 向您的项目添加一个新的 ResourceDictionary(将其命名为 RD1.xaml),将您的模板移入其中.

  1. Add a new ResourceDictionary to your project (name it RD1.xaml), move your Template into it.

使用此签名添加一个新类(将其命名为 RD1.xaml.cs)

Add a new Class (name it RD1.xaml.cs) with this signature

public partial class SomeClassName : ResourceDictionary

  • 将此行代码添加到 ResourceDictionary 标记(在 RD1.xaml 中)

  • Add this line of code to the ResourceDictionary tag (in RD1.xaml)

    x:Class="WpfApplication1.SomeClassName"
    

  • 将这些代码行添加到 App.xaml(如果 RD1 在文件夹中,则添加 FolderName)

  • Add these lines of code to App.xaml (add FolderName if RD1 is in a folder)

    <Application.Resources>
            <ResourceDictionary>
                <ResourceDictionary.MergedDictionaries>
                    <ResourceDictionary Source="FolderName\RD1.xaml"/>
                </ResourceDictionary.MergedDictionaries>
            </ResourceDictionary>
    </Application.Resources>
    

  • 现在您可以在 RD1.xaml.cs 中实现事件处理程序

  • Now you can have your event handler implementation in RD1.xaml.cs

    这篇关于将不同的点击处理程序分配给全局控件模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆