【问题标题】:Expose Custom Control Event that is defined in the Control Template of another Custom Control公开在另一个自定义控件的控件模板中定义的自定义控件事件
【发布时间】:2012-05-25 11:15:25
【问题描述】:

我要做的是公开自定义控件中包含的按钮的单击事件。此自定义控件的实例在另一个自定义控件的控件模板中声明。

例如,假设我有以下内容:

public class CustomTabPanel : Panel
{
    public Button newButton;
    //lots of other stuff
}

然后我有另一个像这样的控件

public class CustomControl : Selector
{
    //stuff here
}

这个 CustomControl 类有一个在 generic.xaml 中定义的模板,它定义了一个 CustomTabPanel 的实例,就像这样(东西已被剥离)

<Style TargetType="{x:Type local:CustomControl}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:CustomControl}">
                    <local:CustomTabPanel IsItemsHost="True"/>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

然后可以在 MainWindow.xaml 或任何地方创建此控件的实例。

我的问题是,假设控件在那里定义,我如何访问 MainWindow.xaml 中 CustomTabPanel 中按钮的 Click 事件?

我试图让这个解释尽可能简单,如果您需要更多的实现细节,请询问。

非常感谢您的帮助!

克里斯

【问题讨论】:

    标签: c# .net wpf xaml


    【解决方案1】:

    将其作为 CustomControl 上的依赖属性公开。 将其连接到您的 CustomControl 代码文件中。它有点利用它。

    【讨论】:

      猜你喜欢
      • 2015-09-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多