DrawingAttributes.AddPropertyData(Guid, Object) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
為物件新增自訂屬性 DrawingAttributes 。
public:
void AddPropertyData(Guid propertyDataId, System::Object ^ propertyData);
public void AddPropertyData(Guid propertyDataId, object propertyData);
member this.AddPropertyData : Guid * obj -> unit
Public Sub AddPropertyData (propertyDataId As Guid, propertyData As Object)
參數
- propertyData
- Object
自訂屬性的值。
propertyData必須是型別 Char、 Byte、 Int16UInt16Int32UInt32Int64UInt64SingleDoubleDateTimeBooleanStringDecimal,或是這些資料型態的陣列;但不能是型別String為 的陣列。
例外狀況
propertyData 是 null。
範例
以下範例示範如何從物件中新增及擷取自訂屬性 DrawingAttributes 。 範例中新增了一個屬性,指示物體是 DrawingAttributes 筆還是螢光筆。 事件處理程式中的ChangeColors_Click程式碼會為使用InkCanvas物件 的筆劃DrawingAttributes渲染新顏色。 inkDA 此範例假設存在一個 InkCanvas 名為 inkCanvas1、 的物件,以及兩個 DrawingAttributes 名為 inkDA、 highlighterDA的對象。
Guid purposeGuid = new Guid("12345678-9012-3456-7890-123456789012");
string penValue = "pen";
string highlighterValue = "highlighter";
// Add a property to each DrawingAttributes object to
// specify its use.
private void AssignDrawingAttributesInstrument()
{
inkDA.AddPropertyData(purposeGuid, penValue);
highlighterDA.AddPropertyData(purposeGuid, highlighterValue);
}
// Change the color of the ink that on the InkCanvas that used the pen.
void ChangeColors_Click(Object sender, RoutedEventArgs e)
{
foreach (Stroke s in inkCanvas1.Strokes)
{
if (s.DrawingAttributes.ContainsPropertyData(purposeGuid))
{
object data = s.DrawingAttributes.GetPropertyData(purposeGuid);
if ((data is string) && ((string)data == penValue))
{
s.DrawingAttributes.Color = Colors.Black;
}
}
}
}
Private purposeGuid As New Guid("12345678-9012-3456-7890-123456789012")
Private penValue As String = "pen"
Private highlighterValue As String = "highlighter"
' Add a property to each DrawingAttributes object to
' specify its use.
Private Sub AssignDrawingAttributesInstrument()
inkDA.AddPropertyData(purposeGuid, penValue)
highlighterDA.AddPropertyData(purposeGuid, highlighterValue)
End Sub
' Change the color of the ink that on the InkCanvas that used the pen.
Private Sub ChangeColors_Click(ByVal sender As [Object], _
ByVal e As RoutedEventArgs)
Dim s As Stroke
For Each s In inkCanvas1.Strokes
If s.DrawingAttributes.ContainsPropertyData(purposeGuid) Then
Dim data As Object = s.DrawingAttributes.GetPropertyData(purposeGuid)
If TypeOf data Is String AndAlso CStr(data) = penValue Then
s.DrawingAttributes.Color = Colors.Black
End If
End If
Next s
End Sub
備註
這個 AddPropertyData 方法讓你能為物件新增自訂屬性 DrawingAttributes 。 當你自己畫筆觸時,這很有用,想提供更多資訊。