C# WPF-具有自定义DependencyProperties的ControlTemplate
我尝试给我的类型按钮的控制模板提供一些依赖属性。为此,我创建了一个名为“ControlButton”的类,并从Button继承了它。我给了这个类一个空构造函数,并试图用包含ControlTemplate的样式连接这个类 下面是我的样式,其中包含ControlTemplate:C# WPF-具有自定义DependencyProperties的ControlTemplate,c#,wpf,styles,dependency-properties,controltemplate,C#,Wpf,Styles,Dependency Properties,Controltemplate,我尝试给我的类型按钮的控制模板提供一些依赖属性。为此,我创建了一个名为“ControlButton”的类,并从Button继承了它。我给了这个类一个空构造函数,并试图用包含ControlTemplate的样式连接这个类 下面是我的样式,其中包含ControlTemplate: <Style TargetType="{x:Type local:ControlButton}" x:Key="ControlButton" xmlns:local="clr-namespac
<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>
使用下面的代码,我尝试使用我的控制按钮
<StackPanel
xmlns:local="clr-namespace:FileZip">
<local:ControlButton
Content="X" />
</StackPanel>
每次我尝试编译代码时,Visual Studio都会返回以下两个错误:
- “名称“ControlButton”在命名空间“FileZip”中不存在”
- 映射指令中缺少XmlNamespace、Assembly或ClrNamespace
很抱歉,如果我的英语不是很好。请阅读解释如何将样式应用于自定义控件的部分。另请阅读如何使用外部部件的示例部分
您的
xmlns
声明应该位于XAML的顶部。不在控制级别。FileZip
位于XAML
所在的同一程序集中,或者位于不同的程序集中?目前只有一个名为FileZip的命名空间。这也是程序集的名称。
<StackPanel
xmlns:local="clr-namespace:FileZip">
<local:ControlButton
Content="X" />
</StackPanel>