我正在开发一个自定义控件,这是到目前为止的内容:
主题/Generic.xaml
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Themes/ColorPicker.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
控件/ColorPicker.cs
namespace TrigDebugUtil.Controls
{
public class ColorPicker : Control
{
#region Private Fields
#endregion //Private Fields
#region Properties
public BitmapImage ColorWheelImage
{
get;
private set;
}
#endregion //Properties
static ColorPicker()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(ColorPicker), new FrameworkPropertyMetadata(typeof(ColorPicker)));
}
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
WriteableBitmap ColorDrawboard;
}
}
}
主题/ColorPicker.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TrigDebugUtil.Controls">
<Style TargetType="{x:Type local:ColorPicker}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:ColorPicker}">
<Border Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<!-- <Image Source="{TemplateBinding ColorWheelImage}" /> -->
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
App.xaml
<Application x:Class="TrigDebugUtil.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:cont="clr-namespace:TrigDebugUtil.Controls"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Themes/Generic.xaml" />
</ResourceDictionary.MergedDictionaries>
<Style TargetType="{x:Type cont:ColorPicker}" BasedOn="{StaticResource ColorPicker}" />
</ResourceDictionary>
</Application.Resources>
</Application>
我现在遇到了很多错误,所有人都这样说:
类型或名称空间“ ColorPicker”在名称空间“ TrigDebugUtil.Controls”中不存在
搜索资源字典“ Themes / Generic.xaml”时发生错误
如果我将相关的行替换为
pack://application:,,,/Trig.DebugUtil;component/Themes/ColorPicker.xaml
该错误被该项目未引用的警告“ Trig.DebugUtil”程序集代替(免费翻译)
不管错误如何,项目似乎都可以编译(控制台输出中没有错误消息,仅在错误列表中)
您应该BasedOn="{StaticResource {x:Type cont:ColorPicker}}"
确定使用。您使用的语法仅适用于WPF提供的默认控件,但对于自定义控件,您必须提供确切的类型。
同样对于TemplateBinding,属性应该是依赖项属性,而不是CLR属性。您已将ColorWheelImage声明为常规CLR属性,但应将其声明为Dependency属性。
句法:
public BitmapImage ColorWheelImage
{
get { return (BitmapImage)GetValue(ColorWheelImageProperty); }
set { SetValue(ColorWheelImageProperty, value); }
}
public static readonly DependencyProperty ColorWheelImageProperty =
DependencyProperty.Register("ColorWheelImage", typeof(BitmapImage),
typeof(ColorPicker));
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句