如何在不同组件类型的WPF样式中正确应用datattrigger绑定

本文关键字:样式 应用 绑定 datattrigger WPF 类型 组件 | 更新日期: 2023-09-27 18:11:16

我对WPF还是个新手。我目前正在研究一个有几个文本框的WPF表单。这些文本框中的每一个都与位于相同x,y坐标上的TextBlock配对,充当GhostText。一旦你点击文本框内的GhostText消失。

下面是一个如何在表单的XAML中设置绑定的示例(所有文本框都重复了相同的代码,因此使用样式的原因):

<TextBox Grid.Column="0" Width="40" Height="25" VerticalAlignment="Top" HorizontalAlignment="Left" x:Name= "RecordMinutesTextBox" Padding="12,5,5,0" Text ="{Binding RecordMinute}"  Margin="0,25,5,1" PreviewTextInput="CheckNumberValidation" Background="{Binding ElementName=FireWashingtonResponseTimeReport,Path=DataContext.RequiredFieldColor}"/>
<TextBlock Grid.Column="0" Width="40"  IsHitTestVisible="False" Text="MIN" VerticalAlignment="Center" HorizontalAlignment="Left" Foreground="DarkGray" Margin="8,25,0,1"                               >
    <TextBlock.Style>
        <Style TargetType="{x:Type TextBlock}">
            <Setter Property="Visibility" Value="Collapsed"/>
            <Style.Triggers>
                <DataTrigger Binding="{Binding Text, ElementName=RecordMinutesTextBox}" Value="">
                    <Setter Property="Visibility" Value="Visible"></Setter>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </TextBlock.Style>
</TextBlock>

*注意其中一个文本框的名称,"RecordMinutesTextBox",用作DataTrigger绑定的ElementName。

下面是我的WPF样式模板中的代码:

<Style x:Key="MinuteAndSecondsGhostText" TargetType="TextBlock">
    <Setter Property="Width" Value="40"/>
    <Setter Property="IsHitTestVisible" Value="False"/>
    <Setter Property="VerticalAlignment" Value="Center"/>
    <Setter Property="HorizontalAlignment" Value="Left"/>
    <Setter Property="Foreground" Value="DarkGray"/>
    <Setter Property="Visibility" Value="Collapsed"/>
    <Style.Triggers>
        <DataTrigger Binding="{Binding Text, ElementName=??WhatDoIPutHere??}" Value="">
            <Setter Property="Visibility" Value="Visible"/>
        </DataTrigger>
    </Style.Triggers>
</Style>enter code here

所以我的问题归结为这个。我应该在DataTrigger绑定中使用什么作为这种样式的ElementName ?考虑到我的表单上有多个具有不同名称的文本框。

如何在不同组件类型的WPF样式中正确应用datattrigger绑定

我给你一个想法…你可以在此基础上修改代码。

在下面的示例中,您可以看到TextBlock显示IsFocused的状态,TextBox的属性。因此,您可以将一对元素放在父元素中,如StackPanel,并通过RelativeSource而不是ElementName访问另一个子元素中的一个子元素的属性…

把这些代码放到你的窗口里,把focus放到TextBox里,你在TextBlock里面看到了什么?

<StackPanel Orientation="Horizontal" Background="White"  Margin="20">
 <TextBox Text="" Name="TextBox" Background="DarkSalmon" Width="100" Height="30"/>
 <TextBlock Text="{Binding Path=Children[0].IsFocused,
                           RelativeSource={RelativeSource Mode=FindAncestor,
                                                          AncestorType={x:Type StackPanel}}}"
               Margin="20,0"/>
</StackPanel>

编辑:

基于我上面的想法,你可以使用RelativeSource代替ElementName来解决你的问题,就像下面的例子:

<Style x:Key="MinuteAndSecondsGhostText" TargetType="TextBlock">
    <Setter Property="Width" Value="40"/>
    <Setter Property="IsHitTestVisible" Value="False"/>
    <Setter Property="VerticalAlignment" Value="Center"/>
    <Setter Property="HorizontalAlignment" Value="Left"/>
    <Setter Property="Foreground" Value="DarkGray"/>
    <Setter Property="Visibility" Value="Collapsed"/>
    <Style.Triggers>
        <DataTrigger Binding="{Binding Path=Children[0].Text,
                               RelativeSource={RelativeSource Mode=FindAncestor,
                                                              AncestorType={x:Type StackPanel}}}" Value="">
            <Setter Property="Visibility" Value="Visible"/>
        </DataTrigger>
    </Style.Triggers>
</Style>

在你的Window正文中:

<StackPanel x:Name="Pair1" Orientation="Horizontal" Background="White"  Margin="20">
    <!--TextBox should be first child of StackPanel-->
    <TextBox Text="" Name="TextBox1" Background="DarkSalmon" Width="100" Height="30"/>
    <TextBlock Text="Sample Text" Style="{StaticResource MinuteAndSecondsGhostText}" Margin="20,0"/>
</StackPanel>
<StackPanel x:Name="Pair2" Orientation="Horizontal" Background="White"  Margin="20">
    <!--TextBox should be first child of StackPanel-->
    <TextBox Text="" Name="TextBox2" Background="DarkSalmon" Width="100" Height="30"/>
    <TextBlock Text="Sample Text" Style="{StaticResource MinuteAndSecondsGhostText}" Margin="20,0"/>
</StackPanel>