我只是在玩网格课,让它成为#34;切肉刀"。所以我希望能够在xaml中使用它,并且只需指定我想要的行数或列等,我就得到了下面的代码。一切正常,我使用加载的事件添加行和列,但现在我在想如何取消订阅此事件?我无法找到如何做的直接信息?
public class MyFormGrid : Grid
{
public static readonly DependencyProperty ColumnsProperty =
DependencyProperty.Register("Columns", typeof(int), typeof(ASLFormGrid), new PropertyMetadata(0));
public static readonly DependencyProperty RowsProperty =
DependencyProperty.Register("Rows", typeof(int), typeof(ASLFormGrid), new PropertyMetadata(0));
public MyFormGrid()
{
Loaded += MyGrid_Loaded;
}
public int Columns
{
get { return (int)GetValue(ColumnsProperty); }
set { SetValue(ColumnsProperty, value); }
}
public int Rows
{
get { return (int)GetValue(RowsProperty); }
set { SetValue(RowsProperty, value); }
}
private void MyGrid_Loaded(object sender, RoutedEventArgs e)
{
for (var i = 0; i < Columns; i++)
{
ColumnDefinitions.Add(new ColumnDefinition());
}
for (var i = 0; i < Rows; i++)
{
RowDefinitions.Add(new RowDefinition(););
}
}
}
亲切的问候
答案 0 :(得分:1)
这可以很容易地放在你加载的事件处理程序中:
public class MyFormGrid : Grid
{
...
public MyFormGrid()
{
Loaded += MyGrid_Loaded;
}
...
private void MyGrid_Loaded(object sender, RoutedEventArgs e)
{
Loaded -= MyGrid_Loaded;
...
}
}