2013-05-15 18 views
0

我想實現一個DataTrigger,比如textBox1。當textBox1內的Text是「ABC」時,我想顯示「數據匹配!」在另一個TextBox說,textBox2。我爲此寫了下面的xaml代碼,但它不工作。我收到以下錯誤消息。此DataTriggers:它是如何工作的

'Text' member is not valid because it does not have a qualifying type name 

XAML代碼:

<Window x:Class="ControlTemplateDemo.Animation" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:sys="clr-namespace:System;assembly=mscorlib" 
    Title="Animation" Height="300" Width="607"> 
<Grid> 
    <Border Background="White"> 
     <StackPanel Margin="30" HorizontalAlignment="Left" Width="500" Height="209"> 
      <TextBox Name="textBox1">           
       <TextBox.Triggers> 
        <DataTrigger Binding="{Binding Path=Text}"> 
         <DataTrigger.Value> 
          <sys:String>ABC</sys:String> 
         </DataTrigger.Value> 
         <Setter TargetName="textBox2" Property="Text" Value="Data matched!"/>        
        </DataTrigger> 
       </TextBox.Triggers> 
      </TextBox>     
      <TextBox Name="textBox2">      
      </TextBox> 
     </StackPanel> 
    </Border> 
</Grid> 

</Window> 

是否有約束力的任何問題嗎?

感謝, 與Hemant

回答

2

你需要給一個StyleDataTrigger第二TextBox

類似:

<StackPanel> 
    <TextBox x:Name="inputBox" /> 
    <TextBox Margin="0 25 0 0"> 
    <TextBox.Style> 
     <Style TargetType="{x:Type TextBox}"> 
     <Setter Property="Text" 
       Value="No Match Found" /> 
     <Style.Triggers> 
      <DataTrigger Binding="{Binding ElementName=inputBox, 
              Path=Text}" 
         Value="ABC"> 
      <Setter Property="Text" 
        Value="Match Found" /> 
      </DataTrigger> 
     </Style.Triggers> 
     </Style> 
    </TextBox.Style> 
    </TextBox> 
</StackPanel> 

TextBox.Triggers不支持DataTrigger。我猜這只是爲EventTriggers作爲文檔狀態

在一個側面說明,我通常有我的綁定在最終作爲目標的元素(盡我所能)。通過這種方式,我發現至少可以親自進行調試。如果TextBox有錯誤的信息,我會立即檢查它的綁定,而不是我xaml文件中的每個綁定,以查看哪個元素具有錯誤的綁定,最終更新我的TextBox

相關問題