我正在创建自定义控件并创建了bindable property
。我想根据这些属性设置子项。 处理此方案的正确方法是什么?我试图寻找任何有意义的覆盖基本控件或我可以连接的事件。
例如,我想在Grid
ColumnCount
和RowCount
时创建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); }
}
}
答案 0 :(得分:1)
是指更新属性时更新列/行?
BindableProperty.Create
具有参数propertyChanged
来处理值更新。
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); }
}
}