Tobias
Tobias

Reputation: 38296

Set Attribute via code-behind to Xaml Object

I want to set an Attribute to a Property, which is defined in Xaml. Here is the Property (for example)

<Converter:PercentageConverter x:Key="percentageConverter" />

and I want to add an Attribute to this Property like this in code-behind:

PercentageConverter percentageConverter = this.Resources["percentageConverter"] as PercentageConverter;
SetAttribute(percentageConverter, XmlIgnoreAttribute);

In the SetAttribute Method is the 'magic' to add the Attribute, that can 'normally be declared like

[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public void PlayPauseCommand(object sender, EventArgs e)
{
    myVlcControl.Play();
}

How can I implement this Magic in the SetAttribute method? I think via Reflection, but how exactly? Is is possible?

Upvotes: 0

Views: 724

Answers (1)

Joe Mancuso
Joe Mancuso

Reputation: 2129

Look into TypeDescriptor.AddAttributes(Object, Attribute[]). It adds class-level attributes to the target component instance.

AddAttributes Method

Upvotes: 1

Related Questions