Reputation: 10865
I'm creating a custom control and created a bindable property
. I wanted to setup the children based on these properties. What is the correct way of handling this scenario? I tried looking for anything that makes sense to override in a base control or events that I can hook up.
For example, I want to create the column/row definitions of a Grid
when I set the ColumnCount
and RowCount
in XAML
public class HeatMap: Grid
{
public HeatMap()
{
// Where should I move these?
Enumerable.Range(1, RowCount)
.ToList()
.ForEach(x => RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto }));
Enumerable.Range(1, ColumnCount)
.ToList()
.ForEach(x => ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Star) }));
}
public static readonly BindableProperty RowCountProperty =
BindableProperty.Create<HeatMap, int>(p => p.RowCount, 0);
public int RowCount
{
get { return (int)GetValue(RowCountProperty); }
set { SetValue(RowCountProperty, value); }
}
public static readonly BindableProperty ColumnCountProperty =
BindableProperty.Create<HeatMap, int>(p => p.ColumnCount, 0);
public int ColumnCount
{
get { return (int)GetValue(ColumnCountProperty); }
set { SetValue(ColumnCountProperty, value); }
}
}
Upvotes: 0
Views: 527
Reputation: 3167
Do you mean update columns/rows on when properties are updated?
BindableProperty.Create
has argument propertyChanged
to handle value updates.
public class HeatMap : Grid
{
public HeatMap()
{
// Where should I move these?
UpdateRows ();
UpdateColumns ();
}
void UpdateColumns ()
{
ColumnDefinitions.Clear ();
Enumerable.Range (1, ColumnCount).ToList ().ForEach (x => ColumnDefinitions.Add (new ColumnDefinition () {
Width = new GridLength (1, GridUnitType.Star)
}));
}
void UpdateRows ()
{
RowDefinitions.Clear ();
Enumerable.Range (1, RowCount).ToList ().ForEach (x => RowDefinitions.Add (new RowDefinition () {
Height = GridLength.Auto
}));
}
public static readonly BindableProperty RowCountProperty =
BindableProperty.Create<HeatMap, int> (p => p.RowCount, 0,
propertyChanged: (bindable, oldValue, newValue) => ((HeatMap)bindable).UpdateRows ());
public int RowCount
{
get { return (int)GetValue(RowCountProperty); }
set { SetValue(RowCountProperty, value); }
}
public static readonly BindableProperty ColumnCountProperty =
BindableProperty.Create<HeatMap, int>(p => p.ColumnCount, 0,
propertyChanged: (bindable, oldValue, newValue) => ((HeatMap)bindable).UpdateColumns ());
public int ColumnCount
{
get { return (int)GetValue(ColumnCountProperty); }
set { SetValue(ColumnCountProperty, value); }
}
}
Upvotes: 1