我想为textBox1实现一个DataTrigger。当textBox1中的Text为"ABC“时,我希望显示"Data matched!”在另一个TextBox中,textBox2。我已经为此写了下面的xaml代码,但它不工作。我看到下面的错误消息。
'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>绑定有问题吗?
谢谢,赫曼特
发布于 2013-05-15 18:45:44
您需要在Style中为第二个TextBox提供DataTrigger
类似于:
<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。
https://stackoverflow.com/questions/16562688
复制相似问题