我试图建立一个WPF自定义控件库,以便可以从不同的WPF软件项目中使用。我使用的是Visual Studio2019。我使用WPF自定义控件库(.NET Framework)模板创建了一个新项目,在其中定义了文本框的子类:
CustomControl1.cs:
using System.Windows;
using System.Windows.Controls;
namespace CCL1
{
public class CustomControl1 : TextBox
{
static CustomControl1()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomControl1), new FrameworkPropertyMetadata(typeof(CustomControl1)));
}
}
}
(稍后将添加功能),该项目中的generic.xaml如下所示:
Generic.xaml:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:CCL1">
<Style TargetType="{x:Type local:CustomControl1}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:CustomControl1}">
<Border Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
这是程序集信息:
AssemblyInfo.cs:
using System.Reflection;
using System.Runtime.InteropServices;
using System.Windows;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("CCL1")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("CCL1")]
[assembly: AssemblyCopyright("Copyright © 2019")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
//In order to begin building localizable applications, set
//<UICulture>CultureYouAreCodingWith</UICulture> in your .csproj file
//inside a <PropertyGroup>. For example, if you are using US english
//in your source files, set the <UICulture> to en-US. Then uncomment
//the NeutralResourceLanguage attribute below. Update the "en-US" in
//the line below to match the UICulture setting in the project file.
//[assembly: NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.Satellite)]
[assembly: ThemeInfo(
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
//(used if a resource is not found in the page,
// or application resource dictionaries)
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
//(used if a resource is not found in the page,
// app, or any theme specific resource dictionaries)
)]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
然后我将此项目添加为对主项目的引用,并希望在主窗口中使用自定义控件。像这样:
MainWindow.xaml:
<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp1"
xmlns:ccl="clr-namespace:CCL1;assembly=CCL1"
mc:Ignorable="d"
Title="MainWindow" Height="Auto" Width="Auto">
<StackPanel>
<TextBox Text="Hello"/>
<ccl:CustomControl1 Height="20" Text="Hello"/>
</StackPanel>
</Window>
正常的TextBox只是用于比较。我可以在设计模式下看到CustomControl1类,它是TextBox的子类,但是当我构建并启动应用程序时,它并不存在。我的搜索告诉我,这是因为找不到自定义控件的主题,尤其是这个问题: WPF Custom Controls Are Invisible 告诉我,如果我在AssemblyInfo的第二个答案中添加详细的行,则它应该起作用-但是,该行已包含在AssemblyInfo中(从一开始就是如此,它是项目模板的一部分),并且没有区别。我可以通过注释掉自定义控件的静态构造函数来显示它,但这似乎是一个残酷的hack,我不想局限于该控件的默认主题,而是希望它按预期工作。
最奇怪的是,几个小时前完成了DID工作,但随后突然改变为我刚才描述的行为。我不记得为实现此目的而进行了特别更改。
很抱歉重新发布之前提出的问题,但是我只是觉得自己在做什么与已经给出的答案有所不同...
编辑:再次重建后,自定义控件现在在设计模式下甚至不可见...
答案 0 :(得分:0)
使用您提供的示例代码,控制模板没有任何显示。您正在设置Text="Hello"
,但没有ContentPresenter
,也没有绑定到Text
属性。
我建议提取TextBox的控件模板,并将其用作自定义控件的起点。