日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

WF,WPF,Silverlight的DependencyProperty 附加属性

發(fā)布時間:2025/7/14 asp.net 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 WF,WPF,Silverlight的DependencyProperty 附加属性 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

WF,WPF,Silverlight的DependencyProperty 附加屬性

注意,是DependencyProperty 附加屬性,而不是綁定屬性

?

?

例子: http://files.cnblogs.com/foundation/Projects.rar

?

首先看一個假設,

在不修改一個類的前提下,如何擴展一個類的成員(繼承也不可以)

這在傳統(tǒng)的OO設計中是無法完成的,但在NET3.0(C#3.0,VB.NET9.0)中提供了擴展方法開實現(xiàn)這一功能

看下例:

場景:我經常要用得到數字除2的結果,如何方便的實現(xiàn)

class Program

{

static void Main(string[] args)

{

double v1 = 123;

double p1 = v1.zzzzz();

System.Console.WriteLine(p1);

?

//--

double p2=(1234567890.123).zzzzz();

System.Console.WriteLine(p2);

}

}

?

static class myExtension

{

public static double zzzzz(this double d)

{

return d/2 ;

}

}

?

這就是擴展方法,在不修改的結構的情況下,為[ double ]添加了[zzzzz]這個方法

linq 用的就是這種方式

?

方法可以這樣做,那屬性哪?

?

升級一下需求,我想一個對象在一個環(huán)境中自動多出某幾個屬性,在另外一個環(huán)境中又自動多出另外幾個屬性

如何做,

先看一下Silverlight

場景:提供一個容器,容器分兩排,放入容器內的件意控件都可設置附加屬性[myTag]的屬性,[myTag]屬性設為[a]的在左邊,設為[b]的在右邊

容器

public class wxdPanel : StackPanel

{

StackPanel p1 = new StackPanel();

StackPanel p2 = new StackPanel();

public wxdPanel()

: base()

{

p1.Width = 200;

p2.Width = 200;

p1.Background = new System.Windows.Media.SolidColorBrush(new Color() { R = 0, G=255, B=0 , A=255});

p2.Background = new System.Windows.Media.SolidColorBrush(new Color() { R = 0, G = 0, B = 255, A = 255 });

this.Orientation = Orientation.Horizontal;

this.Children.Add(p1);

this.Children.Add(p2);

this.Loaded += new RoutedEventHandler(wxdPanel_Loaded);

}

List<FrameworkElement> list = new List<FrameworkElement>();

void wxdPanel_Loaded(object sender, RoutedEventArgs e)

{

foreach (var p in this.Children)

{

if (p is StackPanel)

{

}

else

{

list.Add((FrameworkElement)p);

}

}

?

foreach (var v in list)

{

?

this.Children.Remove(v);

?

if (wxdPanel.GetmyTag(v) == "a")

{

p1.Children.Add(v);

}

if (wxdPanel.GetmyTag(v) == "b")

{

p2.Children.Add(v);

}

}

}

public static string GetmyTag(DependencyObject obj)

{

return (string )obj.GetValue(myTagProperty);

}

public static void SetmyTag(DependencyObject obj, string value)

{

obj.SetValue(myTagProperty, value);

}

public static readonly DependencyProperty myTagProperty = DependencyProperty.RegisterAttached("myTag", typeof(string ), typeof(wxdPanel),new PropertyMetadata(""));

?

}

使用

<UserControl x:Class="SilverlightApplication1.Page"

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

Width="400" Height="300" xmlns:mytype="clr-namespace:SilverlightApplication1">

<mytype:wxdPanel >

<Button Name="Button1" mytype:wxdPanel.myTag="a" Height="53" Width="75" Content="Button1" />

<TextBox Name="TextBox1" mytype:wxdPanel.myTag="a" Height="23" Width="120" Text="TextBox1"/>

<Button Name="Button2" mytype:wxdPanel.myTag="a" Height="53" Width="75" Content="Button2" />

<TextBox Name="TextBox2" mytype:wxdPanel.myTag="b" Height="23" Width="120" Text="TextBox2" />

<Button Name="Button3" mytype:wxdPanel.myTag="a" Height="53" Width="75" Content="Button3" />

<TextBox Name="TextBox3" mytype:wxdPanel.myTag="b" Height="23" Width="120" Text="TextBox3"/>

</mytype:wxdPanel>

?

</UserControl>

?

效果

?

WPF的實現(xiàn)

與Silverlight 相同

再看一下WF的實現(xiàn)

場景:提供一個容器,放入容器內的件意Activity會多出一個明為[myTag]的屬性,[myTag]屬性設為[a]的執(zhí)行,設為[b]的不執(zhí)行

?

先創(chuàng)建這個容器Activity

[Designer(typeof(myDesigner), typeof(IDesigner))]

public partial class myContainer : System.Workflow.ComponentModel.CompositeActivity

{

//-

public static string GetmyTag(object obj)

{

DependencyObject dObject = obj as DependencyObject;

object value = dObject.GetValue(myTagProperty);

return value.ToString();

}

public static void SetmyTag(object obj, string value)

{

DependencyObject dObject = obj as DependencyObject;

dObject.SetValue(myTagProperty, value);

}

public static readonly DependencyProperty myTagProperty = DependencyProperty.RegisterAttached("myTag", typeof(string), typeof(myContainer), new PropertyMetadata(""));

public static string GetleftValue(object obj)

{

DependencyObject dObject = obj as DependencyObject;

object value = dObject.GetValue(leftValueProperty);

return value.ToString();

}

public static void SetleftValue(object obj, object value)

{

DependencyObject dObject = obj as DependencyObject;

dObject.SetValue(leftValueProperty, value);

}

public static readonly DependencyProperty leftValueProperty = DependencyProperty.RegisterAttached("leftValue", typeof(string), typeof(myContainer), new PropertyMetadata(""));

int n = 0;

protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)

{

n = this.EnabledActivities.Count;

?

if (n == 0)

{

return ActivityExecutionStatus.Closed;

}

?

foreach (Activity activity in this.EnabledActivities)

{

n = n - 1;

string l = myContainer.GetmyTag(activity);

?

?

if (l == "a")

{

activity.Closed += new EventHandler<ActivityExecutionStatusChangedEventArgs>(activity_Closed);

executionContext.ExecuteActivity(activity);

}

}

?

return ActivityExecutionStatus.Executing;

}

?

void activity_Closed(object sender, ActivityExecutionStatusChangedEventArgs e)

{

ActivityExecutionContext executionContext = sender as ActivityExecutionContext;

if (n == 0)

{

executionContext.CloseActivity();

}

}

}

?

[ProvideProperty("myTag", typeof(Activity))]

public class myExtenderProvider : IExtenderProvider

{

public bool CanExtend(object extendee)

{

return (((extendee != this) && (extendee is Activity)) && (((Activity)extendee).Parent is myContainer));

}

//-

public string GetmyTag(Activity activity)

{

if (activity.Parent is myContainer)

{

return activity.GetValue(myContainer.myTagProperty).ToString();

}

return null;

}

public void SetmyTag(Activity activity, string value)

{

if (activity.Parent is myContainer)

{

activity.SetValue(myContainer.myTagProperty, value);

}

}

}

?

public class myDesigner : SequenceDesigner

{

protected override void Initialize(Activity activity)

{

base.Initialize(activity);

IExtenderListService service = (IExtenderListService)base.GetService(typeof(IExtenderListService));

if (service != null)

{

bool tag = false;

foreach (IExtenderProvider provider in service.GetExtenderProviders())

{

if (provider.GetType() == typeof(myExtenderProvider))

{

tag = true;

}

}

if (!tag)

{

IExtenderProviderService tempService = (IExtenderProviderService)base.GetService(typeof(IExtenderProviderService));

if (tempService != null)

{

tempService.AddExtenderProvider(new myExtenderProvider());

}

}

}

}

}

?

然后設計工作流

運行結果

?

總結

以上是生活随笔為你收集整理的WF,WPF,Silverlight的DependencyProperty 附加属性的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。