语言

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必须为这些数据类型的类型Char、Byte、、、Int16UInt16Int32UInt32Int64UInt64SingleDoubleDateTime、、Boolean、或StringDecimal数组;但不能是类型的String数组。

例外

propertyData 是 null。

propertyDataId 为空 Guid。

-或-

propertyData 不是节中列出的 Parameters 允许数据类型之一。

示例

以下示例演示如何从 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 对象。 当你呈现自己的笔划并想要提供额外信息时,这非常有用。

适用于