|
自定義控件并不是一項多么難的技術(shù),關(guān)于自定義控件這部分有不少文章講的很透徹,這里我主要把自己練習(xí)自定義控件的過程記錄下來。
這里我以自定義控件BusyPointer為例,首先我們新建一個應(yīng)用程序,命名為CustomControl,這里我們將自定義控件放入單獨的項目中,所以在解決方案里添加一個Silverlight Class Library項目,命名為BusyPointer,現(xiàn)在我們把Class1.cs類刪除,然后在BusyPointer項目中添加一個Silverlight Template Control,我們?yōu)橹麨锽usyPoint,這時架構(gòu)如下圖所示,項目中增加了一個類文件,同時增加了名為Generic的xaml文件。 現(xiàn)在我們開始定義控件邏輯對于BusyPoint這個控件,這里我們只是簡單模仿Win7里一個鼠標行為在BusyPoint.cs文件中給其定義了一個依賴屬性。
public bool IsBusy
{
get
{
return (bool)GetValue(IsBusyProperty);
}
set
{
SetValue(IsBusyProperty, value);
ChangeState();
}
}
private void ChangeState()
{
if (IsBusy) VisualStateManager.GoToState(this, "Busied", true);
else VisualStateManager.GoToState(this, "Normal", true);
}
public static readonly DependencyProperty IsBusyProperty =
DependencyProperty.Register("IsBusy", typeof(bool), typeof(BusyPoint), new PropertyMetadata(new PropertyChangedCallback(OnBusyChanged)));
private static void OnBusyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
((BusyPoint)sender).IsBusy = (bool)e.NewValue;
}
NET技術(shù):Silverlight中自定義控件,轉(zhuǎn)載需保留來源!
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯(lián)系我們修改或刪除,多謝。