admin管理员组文章数量:1122852
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?
本文标签: xamlWPFgridUserControl with two itemsStack Overflow
版权声明:本文标题:xaml - WPF, Grid, UserControl with two items - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736236785a1915776.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论