c# - 是否有一种*干净*的方法可以使只读依赖属性反射(reflect)另一个属性的值?

标签 c# .net wpf data-binding dependency-properties

下面的代码是我目前的解决方案。

我试图模仿的一个很好的例子是 FrameworkElement.ActualWidth 属性。您知道 ActualWidth 属性是如何计算和重新分配的,每当 Width 属性发生变化时,或者每当控件是重绘,还是其他什么时候? ------

从开发者的角度来看,它看起来只是数据绑定(bind)的苦差事。
但是 ActualWidth 是一个只读的依赖属性。微软真的必须通过这个巨大的代码垃圾坑才能让它发挥作用吗?或者是否有更简单的方法来利用数据绑定(bind)系统的现有功能?

public class foo : FrameworkElement
{
    [ValueConversion(typeof(string), typeof(int))]
    public class fooConverter : IValueConverter
    {   public object Convert(  object value, Type targetType,
                                object parameter, CultureInfo culture)
        { ... }
        public object ConvertBack(  object value, Type targetType,
                                    object parameter, CultureInfo culture)
        { ... }
    }

    private static readonly fooConverter fooConv = new fooConverter();



    private static readonly DependencyPropertyKey ReadOnlyIntPropertyKey =
        DependencyProperty.RegisterReadOnly( "ReadOnlyInt", typeof(int),
                                             typeof(foo), null);
    public int ReadOnlyInt
    {   get { return (int)GetValue(ReadOnlyIntPropertyKey.DependencyProperty); }
    }



    public static readonly DependencyProperty ReadWriteStrProperty =
        DependencyProperty.Register( "ReadWriteStr", typeof(string), typeof(foo),
                                     new PropertyMetadata(ReadWriteStr_Changed));
    public string ReadWriteStr
    {   get { return (string)GetValue(ReadWriteStrProperty); }
        set { SetValue(ReadWriteStrProperty, value); }
    }



    private static void ReadWriteStr_Changed(   DependencyObject d,
                                            DependencyPropertyChangedEventArgs e)
    {   try
        {   if (d is foo)
            {   foo f = d as foo;
                f.SetValue( ReadOnlyIntPropertyKey,
                            fooConv.Convert(f.ReadWriteStr, typeof(int), null,
                                            CultureInfo.CurrentCulture));
            }
        }
        catch { }
    }
}

最佳答案

不幸的是,您将需要您拥有的大部分内容。在这种情况下不需要 IValueConverter,因此您可以将其简化为:

public class foo : FrameworkElement
{
    private static readonly DependencyPropertyKey ReadOnlyIntPropertyKey =
        DependencyProperty.RegisterReadOnly( "ReadOnlyInt", typeof(int),
                                         typeof(foo), null);
    public int ReadOnlyInt
    {
       get { return (int)GetValue(ReadOnlyIntPropertyKey.DependencyProperty); }
    }

    public static readonly DependencyProperty ReadWriteStrProperty =
        DependencyProperty.Register( "ReadWriteStr", typeof(string), typeof(foo),
                                 new PropertyMetadata(ReadWriteStr_Changed));

    public string ReadWriteStr
    {
       get { return (string)GetValue(ReadWriteStrProperty); }
        set { SetValue(ReadWriteStrProperty, value); }
    }

    private static void ReadWriteStr_Changed(DependencyObject d,
                                        DependencyPropertyChangedEventArgs e)
    {
         foo f = d as foo;
         if (f != null)
         {
              int iVal;
              if (int.TryParse(f.ReadWriteStr, out iVal))
                  f.SetValue( ReadOnlyIntPropertyKey, iVal);
         }
    }
}

关于c# - 是否有一种*干净*的方法可以使只读依赖属性反射(reflect)另一个属性的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1301495/

相关文章:

c# - 类库(包)中的 Rijndael 不适用于 .NET Core

c# - 使用 C# 从 DataTable 创建 HTML

c# - WPF、MVVM 和 PRISM - 没有为此对象定义无参数构造函数

c# - 在设计时禁止绑定(bind)到静态属性

wpf - ItemTemplate:ListBox 与 ItemsControl

c# - OOPS 概念 : What is the difference in passing object reference to interface and creating class object in C#?

c# - NuGet.targets“无法再次导入。已导入警告

.net - MonoTouch UITableView 问题

c# - hibernate查询使用nolock时出现识别错误

c# - Asp.net将类库中的js编译为嵌入资源,其中包含c#代码