简体   繁体   English

通过后台代码将属性设置为Xaml对象

[英]Set Attribute via code-behind to Xaml Object

I want to set an Attribute to a Property, which is defined in Xaml. 我想将属性设置为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 在SetAttribute方法中是添加属性的“魔术”,通常可以这样声明:

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

How can I implement this Magic in the SetAttribute method? 如何在SetAttribute方法中实现此Magic? I think via Reflection, but how exactly? 我认为是通过反思,但究竟如何呢? Is is possible? 有可能吗?

Look into TypeDescriptor.AddAttributes(Object, Attribute[]). 查看TypeDescriptor.AddAttributes(Object,Attribute [])。 It adds class-level attributes to the target component instance. 它将类级别的属性添加到目标组件实例。

AddAttributes Method AddAttributes方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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