1.對專案新增一個 .cs 檔案:

在此對新增的Class命名為「addedProperty」

2.addProperty.cs最初內容為:
public class addProperty // class name : the property class
{
}
請加入以下內容:
public static readonly DependencyProperty addedMsg =
DependencyProperty.RegisterAttached(
//Name of the property
"addedProperty",
//Type of the property
typeof( bool ),
//Type of the provider of the registered attached property
typeof( addProperty ),
//Callback invoked in case the property value has changed
null );
public static readonly DependencyProperty addedMsg2 =
DependencyProperty.RegisterAttached(
"addedProperty2",
typeof(string),
typeof(addProperty),
null);
3.接著測試程式能否加入此Attached Property,先在主要的 .xmal檔中加入以下內容:
<Grid x:Name="LayoutRoot">
<TextBlock x:Name="myText" Text="message" Loaded="onLoad"/>
</Grid>
4.加入「onLoad」function,簡單的產生一個Silverlight元件以測試結果。:
private void onLoad(object sender, RoutedEventArgs e)
{
myText.SetValue(addProperty.addedMsg, false);
MessageBox.Show(myText.GetValue(addProperty.addedMsg).ToString());
myText.SetValue(addProperty.addedMsg2, "my name is Creat");
MessageBox.Show(myText.GetValue(addProperty.addedMsg2).ToString());
}
在Attached Property裡,第一個屬性為 bool,第二個屬性為 string,程式載入後,以MessageBox顯示我們在執行中加入的Property.
沒有留言:
張貼留言