DrawingAttributes.AddPropertyData(Guid, Object) 方法

定義

將自訂屬性加入至 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)

參數

propertyDataId
Guid

與自訂屬性建立關聯的 Guid

propertyData
Object

自訂屬性的值。 propertyData 必須屬於 CharByteInt16UInt16Int32UInt32Int64UInt64SingleDoubleDateTimeBooleanStringDecimal 類型或這些資料類型的陣列,但不得為類型 String 的陣列。

例外狀況

propertyDatanull

propertyDataId 為空的 Guid

-或-

propertyData 不屬於 Parameters 一節中列出的其中一種允許資料型別。

範例

下列範例示範如何從 DrawingAttributes 物件新增和擷取自訂屬性。 此範例會新增屬性,指出 DrawingAttributes 物件是畫筆還是醒目提示。 事件處理常式中的 ChangeColors_Click 程式碼會針對使用 DrawingAttributes 物件 inkDA 之 上的 InkCanvas 筆劃呈現新的色彩。 這個範例假設有一 inkCanvas1 個名為 的 ,而且有兩 DrawingAttributes 個名為 InkCanvasinkDA 、 和 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 物件。 當您轉譯自己的筆劃並想要提供額外資訊時,這會很有用。

適用於