简体   繁体   English

wpf 中自定义样式的文本框文本始终为空

[英]Text of the textbox is always empty on a customized styling in wpf

Hey I'm designing a new style for a textbox in my WPF application using XAML codes.嘿,我正在使用 XAML 代码为我的 WPF 应用程序中的文本框设计一种新样式。 The textbox is a combination of textbox and textblock, I used the textblock to show name of the textbox when the text is null, and disappears when the text is filled, but there is a problem when I run the app and fill something in the textbox it seems that it's working properly but in the backend when I want to access the textbox Text it's null even though it's filled!!!!文本框是textbox和textblock的组合,我用textblock在文本为空时显示文本框的名称,填充文本时消失,但是运行app并在文本框中填充东西时出现问题似乎它工作正常,但是在后端,当我想访问文本框 Text 时,即使它已填充,它也是空的!!!! Am I doing something wrong from the base or I missed something to do.我是从根本上做错了什么,还是我错过了一些事情要做。

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Style TargetType="{x:Type TextBox}"
           x:Key="TextBoxTheme">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type TextBox}">
                    <Border CornerRadius="10"
                            Background="#353340"
                            Width="200"
                            Height="40">
                        <Grid>
                            <Rectangle StrokeThickness="1"/>
                            <TextBox Margin="1" 
                                     Text="{TemplateBinding Property=Text}"
                                     BorderThickness="0"
                                     Background="Transparent"
                                     VerticalAlignment="Center"
                                     Padding="5"
                                     Foreground="#CFCFCF"
                                     x:Name="textBox"/>
                            <TextBlock IsHitTestVisible="False"
                                       Text="{TemplateBinding Name}"
                                       VerticalAlignment="Center"
                                       HorizontalAlignment="Left"
                                       Margin="10, 0, 0, 0"
                                       FontSize="11"
                                       Foreground="DarkGray">
                                <TextBlock.Style>
                                    <Style TargetType="{x:Type TextBlock}">
                                        <Style.Triggers>
                                            <DataTrigger Binding="{Binding Text, ElementName=textBox}" Value="">
                                                <Setter Property="Visibility" Value="Visible"/>
                                            </DataTrigger>
                                        </Style.Triggers>
                                        <Setter Property="Visibility" Value="Hidden"/>
                                    </Style>
                                </TextBlock.Style>
                            </TextBlock>
                        </Grid>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>

    </Style>
</ResourceDictionary>

Instead of giving the TextBox an area to display its contents, you have overlapped it on top with another TextBox.您没有为 TextBox 提供一个区域来显示其内容,而是将其与另一个 TextBox 重叠在一起。
To display its content, the TextBox looks in the Template for ScrollViewer x:Name="PART_ContentHost" .为了显示其内容,TextBox 在ScrollViewer x:Name="PART_ContentHost"的模板中查找。

Try a Template like this:试试这样的模板:

            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type TextBox}">
                        <Border x:Name="border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
                            <Grid>
                                <TextBlock x:Name="PART_TextBlock" Text="{TemplateBinding Name}" Visibility="Collapsed"/>
                                <ScrollViewer x:Name="PART_ContentHost"
                                          Focusable="false"
                                          HorizontalScrollBarVisibility="Hidden"
                                          VerticalScrollBarVisibility="Hidden"/>
                            </Grid>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="Text"
                                         Value="{x:Static sys:String.Empty}">
                                <Setter TargetName="PART_TextBlock" Property="Visibility" Value="Visible"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>

If you check the documentation for TemplateBinding you'll see it's just syntactic sugar for a one-way RelativeSource binding to the templated parent, so all you need to do is change your TextBox binding to make it two-way:如果您查看TemplateBinding 的文档,您会发现它只是将单向RelativeSource 绑定到模板化父级的语法糖,因此您需要做的就是更改您的 TextBox 绑定以使其具有两种方式:

Text="{Binding Path=Text, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}"

Also, I disagree with the comment above about this not being a good idea, one of the whole purposes of WPF's templating system is to make it possible to template existing controls eg for redistribution of libraries etc. That said, I don't think Name is the best property to use for your hint text, if for no other reason than the fact that it doesn't allow spaces.另外,我不同意上面关于这不是一个好主意的评论,WPF 模板系统的全部目的之一是使现有控件的模板成为可能,例如用于库的重新分发等。也就是说,我不认为Name是用于提示文本的最佳属性,如果没有其他原因,除了不允许空格这一事实。 A much better solution would be to use an attached property .更好的解决方案是使用附加属性 For a quick-and-dirty solution there's also the Tag property, which you're free to use for whatever you want.对于快速而肮脏的解决方案,还有Tag属性,您可以随意使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM