【问题标题】:Custom DependencyProperty with custom datatype具有自定义数据类型的自定义 DependencyProperty
【发布时间】:2012-11-06 20:34:15
【问题描述】:

我想为用户控件创建一个自定义的 DependencyProperty

 public Table Grids
    {
        get { return (Table)GetValue(GridsProperty); }
        set { SetValue(GridsProperty, value); }
    }

    // Using a DependencyProperty as the backing store for Grids.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty GridsProperty =
                DependencyProperty.Register("Grids", typeof(Table), 
                typeof(MyViewer), new UIPropertyMetadata(10));

Here Table 是用于存储行和列的自定义数据类型。这将帮助我像使用它们一样;

<my:MyViewer 
    HorizontalAlignment="Left" 
    Margin="66,54,0,0" 
    x:Name="MyViewer1" 
    VerticalAlignment="Top" 
    Height="400" 
    Width="400"
    Grids="10"/>

<my:MyViewer 
    HorizontalAlignment="Left" 
    Margin="66,54,0,0" 
    x:Name="MyViewer1" 
    VerticalAlignment="Top" 
    Height="400" 
    Width="400"
    Grids="10,20"/>

我尝试将 Table 数据类型定义为;

public class Table 
    {
        public int Rows { get; set; }
        public int Columns { get; set; }

        public Table(int uniform)
        {
            Rows = uniform;
            Columns = uniform;
        }

        public Table(int rows, int columns)
        {
            Rows = rows;
            Columns = columns;
        }
    }

但它不起作用;当我在 XAML 中使用 Grids="10" 时,它会中断。 谁能帮我实现这个目标?

【问题讨论】:

    标签: wpf xaml dependency-properties


    【解决方案1】:

    您在注册方法中设置的默认值不是数据类型不匹配吗?我相信您希望第一个 FrameworkPropertyMetadata 参数类似于:

    new FrameworkPropertyMetadata(new Table())
    

    new FrameworkPropertyMetadata(null)
    

    然后在 XAML 中您可以执行以下操作:

    <my:MyViewer>
        <my:MyViewer.Grids>
            <Table Rows="10" Column="20"/>
        </my:MyViewer.Grids>
    </my:MyViewer> 
    

    【讨论】:

    • 然后在 XAML 中你可以执行以下操作:
    • 谢谢!!我可以通过这种方式实现结果。但是@Clemens 解决方案更具启发性和简单性。
    【解决方案2】:

    属性元数据中的默认值类型错误。这将在加载 MyViewer 类时导致异常。将默认值设置为例如new Table(10).

    除此之外,XAML/WPF 不会通过调用正确的构造函数将字符串 "10""10,20" 自动转换为 Table 类的实例。您必须写一个TypeConverter 来执行此转换。

    一个简单的 TypeConverter 可能如下所示:

    public class TableConverter : TypeConverter
    {
        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        {
            return sourceType == typeof(string);
        }
    
        public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
        {
            string tableString = value as string;
            if (tableString == null)
            {
                throw new ArgumentNullException();
            }
    
            string[] numbers = tableString.Split(new char[] { ',' }, 2);
            int rows = int.Parse(numbers[0]);
            int columns = rows;
    
            if (numbers.Length > 1)
            {
                columns = int.Parse(numbers[1]);
            }
    
            return new Table { Rows = rows, Columns = columns };
        }
    }
    

    TypeConverter 将与您的 Table 类相关联,如下所示:

    [TypeConverter(typeof(TableConverter))]
    public class Table
    {
        ...
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-09-07
      • 1970-01-01
      • 1970-01-01
      • 2016-08-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多