【问题标题】:WPF Custom Control : Events for the controls defined in XAML?WPF 自定义控件:XAML 中定义的控件的事件?
【发布时间】:2015-08-30 06:17:06
【问题描述】:

我正在制作一个从 ComboBox 派生的 CustomControl:

public class CBT : ComboBox
{
    static CBT()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(CBT),
         new FrameworkPropertyMetadata(typeof(CBT)));
    }
}

我正在编辑ComboBox 的默认Style(......来自 Blend 的副本)。

所以,如果我要在 ComboBox 的 Popup 内添加一个 Button,并希望注册其 Click,我应该遵循的最佳方式是什么?

我试过了:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:local="clr-namespace:ui_test"
                xmlns:Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero2"
                x:Class="ui_test.CBT">

并更改为:

public partial class CBT : ComboBox {}

但是会抛出Partial declarations of 'ui_test.CBT' must not specify different base classes的错误

请帮帮我? :) 在此先感谢:)

编辑:我不知道如何处理按钮的事件:(

【问题讨论】:

    标签: wpf xaml combobox custom-controls


    【解决方案1】:

    你需要一种风格,你不能像那样将类设置为资源字典
    看到这个主题类似的问题here 或阅读Mosers guide
    来自文章:

    <Style TargetType="{x:Type yourcontrol}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type yourcontrol}">
                    ... 
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    

    【讨论】:

    • 它们都不能帮助我将 Events 添加到 CustomControl 中定义的控件中......
    【解决方案2】:

    这就是我最终得到的结果: 对于我必须完成的任务,命令是多余的

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            popup = this.GetTemplateChild("PART_Popup") as Popup;
    
        }
    

    只需传入您想要获取的控件的名称:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-20
      • 2010-12-26
      • 1970-01-01
      • 1970-01-01
      • 2020-10-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多