I want to create a custom DependencyProperty for a usercontrol
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 is a custom datatype used to store rows & Columns. That will help me in using them like;
<my:MyViewer
HorizontalAlignment="Left"
Margin="66,54,0,0"
x:Name="MyViewer1"
VerticalAlignment="Top"
Height="400"
Width="400"
Grids="10"/>
or
<my:MyViewer
HorizontalAlignment="Left"
Margin="66,54,0,0"
x:Name="MyViewer1"
VerticalAlignment="Top"
Height="400"
Width="400"
Grids="10,20"/>
I tried to define the Table datatype as;
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;
}
}
but it’s not working; when i use Grids=”10″ in XAML it breaks.
Can anybody help me to achive this?
The default value in the property metadata is of the wrong type. This will result in an exception when the MyViewer class is loaded. Set the default value to e.g.
new Table(10).Besides that, XAML/WPF will not automatically convert the strings
"10"or"10,20"to instances of your Table class by calling the right constructors. You will have to write a TypeConverter to perform this conversion.A simple TypeConverter could look like this:
The TypeConverter would be associated with your Table class like this: