我與衍生自定義控件實驗,我創造了什麼,我認爲將是一個最簡單的推導:派生自定義控件入門?
我在VS 2010中創建一個自定義的控制項目,改變了基類CustomControl1從
Control
到Calendar
。然後我進入Generic.xaml並刪除了爲CustomControl1創建的默認樣式。
最後,我創建了一個WPF應用程序來使用該控件。
當我添加自定義控制應用程序的主窗口,我希望看到一個普通WPF日曆,因爲我已經從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,它位於該控件的主題文件夾:
<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 Fi應受,這裏是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我確實設法看到控制,所以你有什麼是正確的,它可能是一個問題,正確構建它。 – NVM 2010-08-19 18:59:03
謝謝 - 我刪除了之前的問題,因爲它很長很複雜。這是一個更簡單的版本。 – 2010-08-19 19:17:25