Hi,
I'm getting into TPF based control development and I'm finding a lot of difficulties to understand the behind logic maybe because there is not enough documentation and examples on that.
I'm trying to make a control that arrange in a stacklayout many radlabelelement as the Rows property value of my control; the height of any child element should be control.Size.Height/control.Rows, so I got my child to have same height.
This is the way I implement it, could you please help me on that?
MyControl:
MyElement:
I'm getting into TPF based control development and I'm finding a lot of difficulties to understand the behind logic maybe because there is not enough documentation and examples on that.
I'm trying to make a control that arrange in a stacklayout many radlabelelement as the Rows property value of my control; the height of any child element should be control.Size.Height/control.Rows, so I got my child to have same height.
This is the way I implement it, could you please help me on that?
MyControl:
public class MyControl : RadControl |
{ |
MyElementPanel elementPanel; |
public MyControl() |
{ |
this.UseNewLayoutSystem = true; |
this.AutoSize = true; |
} |
protected override void CreateChildItems(RadElement parent) |
{ |
elementPanel = new MyElementPanel(); |
this.RootElement.Children.Add(elementPanel); |
base.CreateChildItems(parent); |
} |
public int Rows |
{ |
get |
{ |
return this.elementPanel.Rows; |
} |
set |
{ |
this.elementPanel.Rows = value; |
} |
} |
} |
MyElement:
public class MyElementPanel : RadItem |
{ |
StackLayoutPanel layout; |
public static RadProperty RowsProperty = RadProperty.Register( |
"Rows", |
typeof(int), |
typeof(MyElementPanel), |
new RadElementPropertyMetadata(2, ElementPropertyOptions.AffectsLayout)); |
public int Rows |
{ |
get |
{ |
return (int)this.GetValue(RowsProperty); |
} |
set |
{ |
this.SetValue(RowsProperty, value); |
} |
} |
protected override void CreateChildElements() |
{ |
var border = new BorderPrimitive(); |
Children.Add(border); |
var background = new FillPrimitive(); |
Children.Add(background); |
layout = new StackLayoutPanel(); |
layout.Orientation = System.Windows.Forms.Orientation.Vertical; |
this.Children.Add(layout); |
var rows = this.Rows; |
int rowHeight = (int)this.Size.Height / rows; |
for (int i = 0; i < rows; i++) |
{ |
var child = new RadLabelElement(); |
child.Text = (i + 1).ToString(); |
child.Margin = new System.Windows.Forms.Padding(1); |
child.Children[2].AutoSize = false; |
child.Children[2].Size = new Size(20, rowHeight); |
child.BorderVisible = true; |
layout.Children.Add(child); |
} |
base.CreateChildElements(); |
} |
protected override void OnPropertyChanged(RadPropertyChangedEventArgs e) |
{ |
if (e.Property == StorageRackElement.RowsProperty) |
{ |
var rows = (int)e.NewValue; |
int rowHeight = (int)this.Size.Height / rows; |
layout.Children.Clear(); |
for (int i = 0; i < rows; i++) |
{ |
var child = new RadLabelElement(); |
child.Text = (i + 1).ToString(); |
child.Margin = new System.Windows.Forms.Padding(1); |
child.Children[2].AutoSize = false; |
child.Children[2].Size = new Size(20, rowHeight); |
child.BorderVisible = true; |
layout.Children.Add(child); |
} |
} |
if (e.Property == StorageRackElement.BoundsProperty) |
{ |
int rowHeight = (int)this.Size.Height / this.Rows; |
foreach(var child in layout.Children) |
child.Children[2].Size = new Size(20, rowHeight); |
} |
base.OnPropertyChanged(e); |
} |
} |