我有一个来自telerik的RadComboBox,它的一些属性有一些受保护的setter。我希望能够设置每个属性,以便从该控件派生出来,并创建了一个自定义控件。我也为它的项目组件做了同样的事情。使用PropertyInfo的SetValue Silverlight的UIElement深层副本?
public class RadComboBoxItem : ListBoxItem
{
...
public bool IsHighlighted
{
get
{
return (bool)GetValue(IsHighlightedProperty);
}
protected set
{
this.SetValue(IsHighlightedPropertyKey, value);
}
}
...
}
public class MyCustomComboBoxItem : RadComboBoxItem
{
public void HighlightItem(bool _default)
{
this.IsHighlighted = _default;
}
}
在我来说,我有RadComboBoxItems的名单,我想创建类型MyCustomComboBoxItem的一个新的列表,这样我就可以从第一个列表基于数据访问的setter每个项目:
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
...
foreach (RadComboBoxItem _item in _listOfRadComboBoxItems)
{
MyCustomComboBoxItem _customCBI = new MyCustomComboBoxItem();
_customCBI.Load(_customCBI.GetType(), _item, true);
_listOfCustomCBI.Add(_newB2);
}
}
}
我发现了另一个岗位上什么,我试图做一个解释,但我的情况有点不同,我借了从这里Load方法:
Updating ObservableCollection Item properties using INotifyPropertyChanged
public static class ExtentionMethods
{
public static void Load<T>(this T target, Type type, T source, bool deep)
{
foreach (PropertyInfo property in type.GetProperties())
{
if (property.CanWrite && property.CanRead)
{
if (!deep || property.PropertyType.IsPrimitive || property.PropertyType == typeof(String))
{
property.SetValue(target, property.GetValue(source, null), null);
}
else
{
object targetPropertyReference = property.GetValue(target, null);
targetPropertyReference.Load(targetPropertyReference.GetType(), property.GetValue(source, null), deep);
}
}
}
}
}
回顾:我在这里要做的是从Telerik的RadComboBox创建一个自定义组合框。这有ComboBoxItems具有IsHighlighted依赖属性设置程序保护。我创建了MyCustomComboBoxItem来绕过这个限制,但我无法将RadComboBoxItem复制到MyCustomComboBoxItem中。
原因:我希望能够设置它,所以我可以帮助用户获得更好的体验。
谢谢。
不宜属性是依赖属性? – 2011-12-28 07:35:58
在我的项目中,受保护的属性是依赖属性。我只是试图简化这个例子。我想要做的是创建一个基于Telerik的RadComboBox的自定义组合框,其中IsHighlighted属性是具有受保护的setter的属性。我需要提供突出显示机制,因为我不想为用户选择一个项目,但我想让他更接近最终选择。 – asuciu 2011-12-28 14:57:10