【发布时间】: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