test, new Binding(),test和Binding啥关系

解决方案 »

  1.   

    Binding只是作为一个提供值的基类。
    Test里的属性先要判断是不是Binding,是Binding就返回Binding的GetValue的值,不是就直接返回值
      

  2.   

    public class Test
    {
        private object _A;
        public object A
        {
            get
            {
                ...
      

  3.   

    但是属性类型不可能只用object啊……谁知道WPF的依赖属性在XAML上是怎么实现的么,WPF的一些绑定会生成BindingExpressionBase,和CLR属性类型不一样,但是载入不会出类型不符的异常……
      

  4.   

    test.A = new Binding();  //这能行吗
      

  5.   

    typeof(Test).GetField("_A").SetValue(test, new Binding());
    WPF绑定也是对内部的依赖项属性进行设置,而依赖项属性都是用object类型存储的,自然不会出现异常。
      

  6.   

    一定要用反射,用的是System.Windows.Markup.XamlReader.Load()。这不是我能控制的……
      

  7.   

    WPF是如何使得XamlReader.Load方法不使用反射去设置属性?
    比如这样的数据。<Test A="{Binding XXX}" />用XamlReader.Load时,它是直接使用反射去SetValue的,会直接报错。
      

  8.   

    你会不会提问啊,你有说是WPF里面使用吗?WPF里面的属性是依赖项属性,你先学学什么叫依赖项属性,根本不是你那样定义的。依赖项属性的赋值和获取根本不使用反射,那样效率太低了。
      

  9.   

    我的意思是,我的类里面定义的属性,XAML是直接通过CLR属性设置的,然后出错。出现的错误和PropertyInfo.SetValue一样。
        [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); } }
        }
      

  10.   

    看来还是得要写一个TypeDescriptionProvider和ICustomTypeDescriptor是吧……
      

  11.   

    你给的几张图,真的没看出有什么必然的联系。
    WPF中的Binding是通过SetBinding与依赖项属性关联的,而不是作为值赋值给某个属性,你却将自己定义的一个名为Binding的类,作为值赋值给某个属性,自然要出错,运行原理是不同的。
      

  12.   

    问题错在,Test.A属性为STRING,而typeof(Test).GetProperty("A").SetValue(test, new Binding());是将对象(new Binding())赋给STRING,肯定报错的!不支持隐式转换,需将Test.A属性改为object即可
      

  13.   

    同样是XamlReader.Load,WPF是怎么把XAML里的绑定属性值绑定到Object的?
    我现在写了个TypeDescriptionProvider,但是没有被调用……
      

  14.   

    PropertyInfo.PropertyType.Name你是不是要这个属性的声明类型名称哇?
    取到类型名称后,就可以判断转换你想要的动作了?