我想为用户控件创建一个自定义的 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));
这里的 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" 时,它会中断。任何人都可以帮助我实现这一目标吗?