简体   繁体   English

文本框上的XAML Controltemplate,文本框上的Datatrigger

[英]XAML Controltemplate on Textbox, Datatrigger on textbox

I've create a custom TextBox , I want to style this, and add a Watermark to this TextBox . 我创建了一个自定义TextBox ,我想要设置它的样式,并为此TextBox添加一个Watermark。 I want to have the watermark added when the TextBox is empty, this visiblity should only be set from my Style . 我想在TextBox为空时添加水印,这个可见性只能从我的Style设置。 This is my style: 这是我的风格:

<Style TargetType="{x:Type textboxes:CustomTextBox}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type textboxes:CustomTextBox}">
                <Grid>
                    <Border Name="Border"
                            CornerRadius="2"
                            Padding="2"
                            Background="DeepPink"
                            BorderBrush="Green"
                            BorderThickness="1">
                        <Grid>
                            <ScrollViewer Margin="0"
                                          x:Name="PART_ContentHost" />
                            <TextBlock IsHitTestVisible="False"
                                       Text="WATERMARK!"
                                       Foreground="White"
                                       VerticalAlignment="Center"
                                       HorizontalAlignment="Left"
                                       Margin="10,0,0,0">
                                <TextBlock.Style>
                                    <Style TargetType="{x:Type TextBlock}">
                                        <Setter Property="Visibility"
                                                Value="Collapsed" />
                                        <Style.Triggers>
                                            <DataTrigger Binding="{Binding Text, ElementName=PART_ContentHost}"
                                                         Value="">
                                                <Setter Property="Visibility"
                                                        Value="Visible" />
                                            </DataTrigger>
                                        </Style.Triggers>
                                    </Style>
                                </TextBlock.Style></TextBlock>
                        </Grid>
                    </Border>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

The style is applied as expected, but I struggle to define the DataTrigger binding inside the TextBlock . 样式按预期应用,但我很难在TextBlock定义DataTrigger绑定。 What should I Bind to? 我应该绑定什么?

you should use ControlTemplate triggers instead. 你应该使用ControlTemplate触发器。 You need to check if text is empty but also if textbox doest not have focus, so multitrigger is required: 您需要检查文本是否为空,但如果文本框没有焦点,则需要多项组合:

here is my watermark textbox style where I use Tag property to specify watermark text: 这是我的水印文本框样式,我使用Tag属性指定水印文本:

<Style x:Key="WatermarkTextBox" TargetType="TextBox" BasedOn="{StaticResource {x:Type TextBox}}">
    <Setter Property="Tag" Value="Enter Text" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="TextBox">
                <Grid>
                    <Border x:Name="border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
                        <ScrollViewer x:Name="PART_ContentHost" Focusable="False" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden"/>
                    </Border>

                    <TextBlock x:Name="Watermark" Text="{TemplateBinding Tag}" 
                            Visibility="Collapsed"  IsHitTestVisible="False" Opacity="0.5"
                            Foreground="{TemplateBinding Foreground}" 
                            Margin="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}"
                            HorizontalAlignment="Stretch"
                            VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                            TextAlignment="{TemplateBinding TextAlignment}"/>
                </Grid>
                <ControlTemplate.Triggers>
                    <MultiTrigger>
                        <MultiTrigger.Conditions>
                            <Condition Property="IsKeyboardFocusWithin" Value="False" />
                            <Condition Property="Text" Value="" />
                        </MultiTrigger.Conditions>
                        <Setter TargetName="Watermark" Property="Visibility" Value="Visible" />
                    </MultiTrigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

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

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