|
自定義控件并不是一項(xiàng)多么難的技術(shù),關(guān)于自定義控件這部分有不少文章講的很透徹,這里我主要把自己練習(xí)自定義控件的過(guò)程記錄下來(lái)。
這里我以自定義控件BusyPointer為例,首先我們新建一個(gè)應(yīng)用程序,命名為CustomControl,這里我們將自定義控件放入單獨(dú)的項(xiàng)目中,所以在解決方案里添加一個(gè)Silverlight Class Library項(xiàng)目,命名為BusyPointer,現(xiàn)在我們把Class1.cs類刪除,然后在BusyPointer項(xiàng)目中添加一個(gè)Silverlight Template Control,我們?yōu)橹麨锽usyPoint,這時(shí)架構(gòu)如下圖所示,項(xiàng)目中增加了一個(gè)類文件,同時(shí)增加了名為Generic的xaml文件。 現(xiàn)在我們開(kāi)始定義控件邏輯對(duì)于BusyPoint這個(gè)控件,這里我們只是簡(jiǎn)單模仿Win7里一個(gè)鼠標(biāo)行為在BusyPoint.cs文件中給其定義了一個(gè)依賴屬性。
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)載需保留來(lái)源!
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。