【发布时间】:2010-08-19 18:51:52
【问题描述】:
我正在试验派生的自定义控件,并创建了我认为可能是最简单的派生:
我在 VS 2010 中创建了一个自定义控件项目,并将 CustomControl1 的基类从
Control更改为Calendar。然后我进入 Generic.xaml 并删除了为 CustomControl1 创建的默认样式。
最后,我创建了一个 WPF 应用程序来使用该控件。
当我将自定义控件添加到应用程序的 MainWindow 时,我预计会看到一个常规的 WPF 日历,因为我是从 Calendar 派生的,并且没有对 Calendar 控件模板进行任何更改。
相反,在设计时或运行时没有任何显示。 MainWindow 保持为空。我不确定发生了什么,但很明显我在某个地方做了错误的假设。
谁能帮我解决这个问题?感谢您的帮助。
顺便说一句——我为什么要这样做?我正在扩展 Calendar 控件,但我只需要修改 CalendarDayButton 控件模板。在进行修改之前,我想我应该能够先显示未修改的日历。就像我说的那样,我认为我在某个地方做出了错误的假设。
CustomControl1.cs 这是 CustomControl1 的代码:
using System.Windows;
using System.Windows.Controls;
namespace WpfCustomControlLibrary1
{
public class CustomControl1 : Calendar
{
static CustomControl1()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomControl1), new FrameworkPropertyMetadata(typeof(CustomControl1)));
}
}
}
Generic.xaml 这是 Generic.xaml 的标记,位于控件的 Themes 文件夹中:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfCustomControlLibrary1">
</ResourceDictionary>
主窗口 最后,这是 MainWindow.xaml 标记:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:WpfCustomControlLibrary1="clr-namespace:WpfCustomControlLibrary1;assembly=WpfCustomControlLibrary1" Title="MainWindow" Height="350" Width="525">
<Grid>
<WpfCustomControlLibrary1:CustomControl1 />
</Grid>
</Window>
WpfApplication1 包含对 WpfCustomControlLibrary1 项目的引用。
【问题讨论】:
-
通过我实际上从您删除的问题中下载了 FSCalender 项目,虽然我没有 VS2010,但我确实设法在设计器中看到了控件,所以您所拥有的是正确的,它可能是正确构建它的问题。
-
谢谢——我删除了之前的问题,因为它很长而且很复杂。这是一个更简单的版本。
标签: wpf custom-controls