111,092
社区成员




public class Test
{
private object _A;
public string A
{
get
{
if (_A is Binding)
return (string)((Binding)_A).GetValue();
return (string)_A;
}
set
{
_A = value;
}
}
}
public class Binding
{
public object GetValue() { return null; }
}
static void Main(string[] args)
{
Test test = new Test();
typeof(Test).GetProperty("A").SetValue(test, new Binding());
//报错
}
[ContentProperty("ItemsSource")]
public class ItemsControl : Control
{
public ItemsControl()
{
_Items = new ItemCollection(this);
}
public static readonly DependencyProperty ItemTemplateProperty = DependencyProperty.Register("ItemTemplate", typeof(DataTemplate), typeof(ItemsControl));
public DataTemplate ItemTemplate { get { return (DataTemplate)GetValue(ItemTemplateProperty); } set { SetValue(ItemTemplateProperty, value); } }
public static readonly DependencyProperty ItemTemplateSelectorProperty = DependencyProperty.Register("ItemTemplateSelector", typeof(DataTemplateSelector), typeof(ItemsControl));
public DataTemplateSelector ItemTemplateSelector { get { return (DataTemplateSelector)GetValue(ItemTemplateSelectorProperty); } set { SetValue(ItemTemplateSelectorProperty, value); } }
private ItemCollection _Items;
[Category("Common Properties")]
public ItemCollection Items { get { return _Items; } }
internal Panel ItemsPanelElement;
public static readonly DependencyProperty ItemsPanelProperty = DependencyProperty.Register("ItemsPanel", typeof(ItemsPanelTemplate), typeof(ItemsControl));
public ItemsPanelTemplate ItemsPanel { get { return (ItemsPanelTemplate)GetValue(ItemsPanelProperty); } set { SetValue(ItemsPanelProperty, value); } }
public static readonly DependencyProperty ItemsSourceProperty = DependencyProperty.Register("ItemsSource", typeof(IEnumerable), typeof(ItemsControl));
[Category("Common Properties")]
public IEnumerable ItemsSource { get { return (IEnumerable)GetValue(ItemsSourceProperty); } set { SetValue(ItemsSourceProperty, value); } }
}
<Test A="{Binding XXX}" />
用XamlReader.Load时,它是直接使用反射去SetValue的,会直接报错。