我想在每次与它相关的文本框聚焦时在Label上引发一个触发器。我做到了这一点,但我有很多形式的标签。在资源方面有什么办法吗,我将不胜感激任何帮助和感谢。我想出使用Tag将文本框名称传递给触发器,但我不知道该怎么做!
<Label Grid.Column="0"
Grid.Row="0"
Content="{StaticResource CIN}"
Tag="">
<Label.Style>
<Style TargetType="Label">
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=TxCIN, Path=IsFocused}" Value="true">
<Setter Property="FontWeight" Value="SemiBold"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Label.Style>
</Label>
<TextBox Grid.Column="1"
Grid.Row="0"
Name="TxCIN"/>
我为你创建了一个样本:
<Window.Resources>
<Style x:Key="LabelStyle" TargetType="{x:Type Label}">
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType={x:Type StackPanel}}, Path=Children[1].IsFocused}" Value="true">
<Setter Property="Background" Value="Red"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Window.Resources>
<Grid x:Name="MainGrid">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<StackPanel Orientation="Horizontal" Grid.Row="0">
<Label Content="First Name:" Style="{StaticResource LabelStyle}"/>
<TextBox Width="150"/>
</StackPanel>
<StackPanel Orientation="Horizontal" Grid.Row="1">
<Label Content="Last Name:" Style="{StaticResource LabelStyle}"/>
<TextBox Width="150"/>
</StackPanel>
<StackPanel Orientation="Horizontal" Grid.Row="2">
<Label Content="Address:" Style="{StaticResource LabelStyle}"/>
<TextBox Width="150"/>
</StackPanel>
<StackPanel Orientation="Horizontal" Grid.Row="3">
<Label Content="Zip Code:" Style="{StaticResource LabelStyle}"/>
<TextBox Width="150"/>
</StackPanel>
</Grid>
如果您看到,则会将共同样式应用于所有标签。我的祖先是StackPanel,TextBox是第二个兄弟,因此Path = Children [1] .IsFocused。请根据您的xaml更改AncestorType和Children的索引。