For settings, I frequently use with two columns: TextBlock for header/label and TextBox for value. It could be done in , but I want to have columns aligned.
E.g.
<TextBlock Text="Server" Grid.Row="0"/>
<TextBox Name="uiFtpServer" Grid.Row="0" Grid.Column="1"/>
I want to create UserControl for this. I almost did it :) Grid.Children contains TextBlock, but it is not displayed (Height=Width=0).
<local:GridedTBox x:Name="uiFtpServer" Label="Server" Grid.Row="0"/>
and
Public Class GridedTBox
Inherits TextBox
' making GridedTBox inherited from TextBox, I have all TextBox properties available on GridedTBox level
Private _Label As New TextBlock
Public Property Label As String
Get
Return _Label.Text
End Get
Set(value As String)
_Label.Text = value
End Set
End Property
Public Overrides Sub OnApplyTemplate()
Dim gridek As Grid = TryCast(Me.VisualParent, Grid)
If gridek Is Nothing Then Return
gridek.Children.Add(_Label)
' _Label is added, but height=NaN, desiredsize=0,0, actualwidht=0, etc.
If Grid.GetColumn(Me) > 0 Then
Grid.SetColumn(_Label, Grid.GetColumn(Me) - 1)
Else
Grid.SetColumn(Me, 1)
Grid.SetColumn(_Label, 0)
End If
_Label.ApplyTemplate()
MyBase.OnApplyTemplate()
' still _Label has height=NaN, desiredsize=0,0, actualwidht=0, etc.
' now, debugging says that Grid.Columns has proper values (i.e. 1 for TextBox, 0 for TextBlock)
' but TextBox on screen is in column 0, so something changes it after this OnApplyTemplate
End Sub
End Class
Questions: what am I doing wrong? Should I use another EventHandler?