【问题标题】:WPF - ControlTemplate with custom DependencyPropertiesWPF - 具有自定义 DependencyProperties 的 ControlTemplate
【发布时间】:2013-10-13 17:13:18
【问题描述】:

我尝试为我的 ControlTemplate 类型的 Button 提供一些 DependencyProperties。为此,我创建了一个名为“ControlButton”的类,并从 Button 继承了它。我给这个类一个空的构造函数,并尝试将这个类与包含 ControlTemplate 的样式连接起来。

这是我的 Style,其中包含 ControlTemplate:

<Style
    TargetType="{x:Type local:ControlButton}"
    x:Key="ControlButton"
    xmlns:local="clr-namespace:FileZip">
    <Setter
        Property="Template">
        <Setter.Value>
            <ControlTemplate
                TargetType="{x:Type local:ControlButton}">
                <Border>
                    <!-- ... -->
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

这是我的 class,应该可以帮助我添加一些 DependencyProperties。我没有添加 DependencyProperties,因为我想看看类和样式之间的联系是否有效:

namespace FileZip {
    public partial class ControlButton : Button {
        public ControlButton () : base() {}
    }
}

通过以下代码,我尝试使用我的 ControlButton

<StackPanel
    xmlns:local="clr-namespace:FileZip">
    <local:ControlButton
        Content="X" />
</StackPanel>

每次我尝试编译我的代码时,Visual Studio 都会返回以下两个错误:

  • “名称‘ControlButton’不存在于命名空间‘FileZip’中”
  • “映射指令中缺少 XmlNamespace、Assembly 或 ClrNamespace”

提前感谢您的帮助。

对不起,如果我的英语不是很好。

【问题讨论】:

  • 您的 xmlns 声明应位于 XAML 的顶部。不在控制级别。
  • FileZip 在您的XAML 所在的同一程序集中还是在不同的程序集中?
  • 目前只有一个名为 FileZip 的命名空间。这也是程序集的名称。

标签: c# wpf styles dependency-properties controltemplate


【解决方案1】:

阅读解释如何将样式应用于自定义控件的部分。另请阅读示例如何使用外部程序集的部分。

http://msdn.microsoft.com/en-us/library/cc295235.aspx

【讨论】:

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