如何在WPF中编辑旧模板
我尝试创建从TextBox继承的新自定义控件,名为OltholderTextbox。 So I added a TextBlock directly above the TextBox using Template :
<!-- PlaceHolderTextBox template -->
<ControlTemplate TargetType="TextBox">
<Grid>
<TextBlock x:Name=""holder"" />
<ScrollViewer x:Name=""PART_ContentHost"" Margin=""0"" />
</Grid>
</ControlTemplate>
All are ok for now, the real TextBox will appear in the ScrollViewer automatically.现在,我想得出此类来创建我的AutoCompleteTextBox Control,从OlacholderTextbox继承了继承,因此我必须再次编辑模板,但是如何像上面的ScrollViewer一样将基本控制量载箱添加到模板中? !!
<!-- AutocompleteTextBox template -->
<Grid>
<PlaceHolderTextBox Name=""tbTextBox""/>
<Popup x:Name=""pop""
Visibility=""Visible""
StaysOpen=""False""
Placement=""Bottom"">
<ListBox x:Name=""lbSuggestions""
Height=""135""
/>
</Popup>
</Grid>
在模板中显示的OptrolderTextBox控件中的问题将被视为新控件(不绑定到继承的属性(如文本)),而不像第一类中的scrollviewer一样。
I tried to use PART_XXX mechanism in the base class to solve this issue, but I failed to edit incoming template -like ScrollViewer did- before use :
Public Class PlaceHolderTextBox
Inherits TextBox
Public Overrides Sub OnApplyTemplate()
Dim host = Me.GetTemplateChild("PART_PlaceHolderContentHost")
If host IsNot Nothing AndAlso TypeOf host Is Grid Then
Dim xaml = "<Grid>
<TextBlock x:Name=""holder"" />
<Grid x:Name=""PART_ContentHost"" Margin=""0"" />
</Grid>"
Dim strReader = New StringReader(xaml)
Dim xmlReader = Xml.XmlReader.Create(strReader)
Dim grid As Grid = XamlReader.Load(xmlReader)
CType(host, Grid).Children.Add(grid)
End If
MyBase.OnApplyTemplate()
End Sub
End Class
So I added new controls to the grid (PART_PlaceHolderContentHost) but ScrollViewer here will not work correctly because it添加到真实网格(不是模板)中。因此,如何编辑传入的模板以使ScrollViewer视为TextBoxBase。
I trying to create new custom control named PlaceHolderTextBox inherited from TextBox. So I added a TextBlock directly above the TextBox using Template :
<!-- PlaceHolderTextBox template -->
<ControlTemplate TargetType="TextBox">
<Grid>
<TextBlock x:Name=""holder"" />
<ScrollViewer x:Name=""PART_ContentHost"" Margin=""0"" />
</Grid>
</ControlTemplate>
All are ok for now, the real TextBox will appear in the ScrollViewer automatically. Now I want to derive this class to create my AutocompleteTextBox control inherited from PlaceHolderTextBox, so I have to edit template again, but how can I add the base control PlaceHolderTextBox to the template like I did by ScrollViewer above? !!
<!-- AutocompleteTextBox template -->
<Grid>
<PlaceHolderTextBox Name=""tbTextBox""/>
<Popup x:Name=""pop""
Visibility=""Visible""
StaysOpen=""False""
Placement=""Bottom"">
<ListBox x:Name=""lbSuggestions""
Height=""135""
/>
</Popup>
</Grid>
The problem in the PlaceHolderTextBox control shown in the template, it will be treated as new control (not bound to the inherited properties like Text) and not like the ScrollViewer in the first class.
I tried to use PART_XXX mechanism in the base class to solve this issue, but I failed to edit incoming template -like ScrollViewer did- before use :
Public Class PlaceHolderTextBox
Inherits TextBox
Public Overrides Sub OnApplyTemplate()
Dim host = Me.GetTemplateChild("PART_PlaceHolderContentHost")
If host IsNot Nothing AndAlso TypeOf host Is Grid Then
Dim xaml = "<Grid>
<TextBlock x:Name=""holder"" />
<Grid x:Name=""PART_ContentHost"" Margin=""0"" />
</Grid>"
Dim strReader = New StringReader(xaml)
Dim xmlReader = Xml.XmlReader.Create(strReader)
Dim grid As Grid = XamlReader.Load(xmlReader)
CType(host, Grid).Children.Add(grid)
End If
MyBase.OnApplyTemplate()
End Sub
End Class
So I added new controls to the grid (PART_PlaceHolderContentHost) but ScrollViewer here will not work correctly because it added to the real grid (not template). So how can I edit incoming template to make ScrollViewer treated as TextBoxBase.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论