gpt4 book ai didi

c# - 公开在另一个自定义控件的控件模板中定义的自定义控件事件

转载 作者:太空宇宙 更新时间:2023-11-03 16:30:59 26 4
gpt4 key购买 nike

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

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

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 中 Button 的 Click 事件?

我尽量使解释尽可能简单,如果您需要更多实现细节,请询问。

非常感谢您的帮助!

克里斯

最佳答案

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

关于c# - 公开在另一个自定义控件的控件模板中定义的自定义控件事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10753441/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com