简体   繁体   中英

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?

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

AddAttributes Method

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM