如何在WPF中编辑旧模板

发布于 2025-01-21 07:58:10 字数 2223 浏览 2 评论 0原文

我尝试创建从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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文