Wpf 具有自定义数据类型的自定义DependencyProperty

Wpf 具有自定义数据类型的自定义DependencyProperty,wpf,xaml,dependency-properties,Wpf,Xaml,Dependency Properties,我想为usercontrol创建自定义DependencyProperty public Table Grids { get { return (Table)GetValue(GridsProperty); } set { SetValue(GridsProperty, value); } } // Using a DependencyProperty as the backing store for Grids. This enab

我想为usercontrol创建自定义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));
这里的表是用于存储行和列的自定义数据类型。这将有助于我像这样使用它们

<my:MyViewer 
    HorizontalAlignment="Left" 
    Margin="66,54,0,0" 
    x:Name="MyViewer1" 
    VerticalAlignment="Top" 
    Height="400" 
    Width="400"
    Grids="10"/>
但它不起作用;当我在XAML中使用Grids=“10”时,它会中断。
有人能帮我做到这一点吗?

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

new FrameworkPropertyMetadata(new Table())

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

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

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

new FrameworkPropertyMetadata(new Table())

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

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

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

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

简单的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(typeof(TableConverter))]
public class Table
{
    ...
}
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(typeof(TableConverter))]
public class Table
{
    ...
}

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

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

简单的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(typeof(TableConverter))]
public class Table
{
    ...
}
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(typeof(TableConverter))]
public class Table
{
    ...
}

然后在XAML中,您可以执行以下操作:谢谢!!用这种方法我能达到目的。然而@Clemens解决方案更具诱导性和简单性。然后在XAML中,您可以执行以下操作:谢谢!!用这种方法我能达到目的。然而@Clemens解决方案更具诱导性和简单性。