【问题标题】:XAML Binding to complex value objectsXAML 绑定到复杂值对象
【发布时间】:2011-02-28 17:44:41
【问题描述】:

我有一个复杂的值对象类,它具有 1) 数字或只读属性; 2) 私有构造函数;和 3) 一些静态单例实例属性 [因此 ComplexValueObject 的属性永远不会改变,并且单个值在应用程序的生命周期中被实例化一次]。

public class ComplexValueClass
{
    /* A number of read only properties */
    private readonly string _propertyOne;
    public string PropertyOne
    {
        get
        {
            return _propertyOne;
        }
    }

    private readonly string _propertyTwo;
    public string PropertyTwo
    {
        get
        {
            return _propertyTwo;
        }
    }

    /* a private constructor */
    private ComplexValueClass(string propertyOne, string propertyTwo)
    {
        _propertyOne = propertyOne;
        _propertyTwo = PropertyTwo;
    }

    /* a number of singleton instances */
    private static ComplexValueClass _complexValueObjectOne;
    public static ComplexValueClass ComplexValueObjectOne
    {
        get
        {
            if (_complexValueObjectOne == null)
            {
                _complexValueObjectOne = new ComplexValueClass("string one", "string two");
            }
            return _complexValueObjectOne;
        }
    }

    private static ComplexValueClass _complexValueObjectTwo;
    public static ComplexValueClass ComplexValueObjectTwo
    {
        get
        {
            if (_complexValueObjectTwo == null)
            {
                _complexValueObjectTwo = new ComplexValueClass("string three", "string four");
            }
            return _complexValueObjectTwo;
        }
    }
}

我有一个看起来像这样的数据上下文类:

public class DataContextClass : INotifyPropertyChanged
{


    private ComplexValueClass _complexValueClass;
    public ComplexValueClass ComplexValueObject
    {
        get
        {
            return _complexValueClass;
        }
        set
        {
            _complexValueClass = value;
            PropertyChanged(this, new PropertyChangedEventArgs("ComplexValueObject"));
        } 
    }
}  

我想将 XAML 绑定语句写入我的复杂值对象上的属性,当整个复杂值对象发生更改时,它会更新 UI。这样做的最佳和/或最简洁的方法是什么?我有类似的东西:

<Object Value="{Binding ComplexValueObject.PropertyOne}" />

但当 ComplexValueObject 整体发生变化时,UI 不会更新。

【问题讨论】:

    标签: c# wpf xaml oop binding


    【解决方案1】:

    您的 Complex 类需要 INotifyPropertyChanged。唯一的通知是如果您重新分配父类中的整个属性,则该子类的属性需要通知 to0 如果您要绑定到它们。

    【讨论】:

      【解决方案2】:

      您的原始方案应该可以正常工作,因为在大多数情况下,绑定可以识别其属性路径任何部分的更改通知。实际上,我尝试了您发布的代码以进行确认,它确实可以正常工作。

      在您的精简样本中,您是否还可能没有表达其他复杂性?我能想到的第一个是 collections->ItemsSource Bindings 但可能与您分配绑定值的属性相关(因为它显然不是对象)或完全其他的东西。

      【讨论】:

        【解决方案3】:

        您不会通知 PropertyOne 的更改,因此 UI 不会更新。而是绑定到 ComplexValueObject 并使用值转换器来获取属性值。

        <Object Value="{Binding Path=ComplexValueObject, Converter={StaticResource ComplexValueConverter}, ConverterParameter=PropertyOne}" /> 
        
        public class ComplexValueConverter : IValueConverter
        {
          public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
          {
            ComplexValue cv = value as ComplexValue;
            string propName = parameter as string;
            switch (propName)
            {
              case "PropertyOne":
                return cv.PropertyOne;
              case "PropertyTwo":
                return cv.PropertyTwo;
              default:
                throw new Exception();
            }
          }
        
          public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
          {
            throw new NotImplementedException();
          }
        }
        

        【讨论】:

        • 确实有效。我想知道是否有一种纯 XAML 方式来完成同样的事情。
        • 你的意思是完全没有代码,保持你的数据类不变???编写一个具有 ComplexValueClass 类型的依赖属性的用户控件,当依赖属性发生变化时,它将使自身失效。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-08-31
        • 2021-10-06
        相关资源
        最近更新 更多