2015-09-05 61 views
0

当谈到WPF时,我仍然略显绿色。我目前正在研究一个WPF表单,其上有几个文本框。每个TextBox都与坐在同一个x,y坐标中的TextBlock配对,作为GhostText。一旦你在文本框内单击,GhostText消失。如何在不同的组件类型中正确应用WPF样式中的DataTrigger绑定?

下面的最初结合是建立在窗体的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 Binding中使用什么作为此样式的ElementName?考虑到我的表单上有多个不同名称的文本框。提前致谢。

+0

@ThomasBomans我很欣赏编辑建议。谢谢。 – ARH

回答

0

我给你一个主意...你可以根据它改变你的代码。

在我的以下示例中,您看到TextBlock显示TextBox的状态为IsFocused属性。所以,你可以把你对元素的父母一样StackPanel并获得在另一个孩子一个孩子的性质由RelativeSource代替ElementName ...

只要把你的窗口,这个代码在TextBoxfocus,你什么看到里面的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> 
+0

感谢您的想法。我正在尝试它。如果它符合我的需求,我会通知您。 – ARH

+0

不幸的是,这并不能解决我的问题。通过这种方式使用TextBlock中的文本属性可以删除我的Ghost文本功能。我需要弄清楚的代码与TextBlock的DataTrigger相关。 – ARH

+0

你写道:_一旦你单击文本框内的GhostText消失._ Fisrt请告诉我**这是你想要的?**如果这是你想要的,然后使用'RelativeSource'而不是'ElementName'类似我的代码。我将第一个子元素('TextBox')的'IsFocused'属性绑定到'TextBlock'的'Text'属性。您可以通过'BooleanToVisibilityConverter'将'IsFocused'绑定到'Visibility'属性。你需要更多的帮助来实现这个吗? – RAM

相关问题