WPF:如何在目标TextBox具有焦点时设置标签样式?

时间:2011-03-28 18:00:10

标签: binding

我想在关联的文本框具有焦点时突出显示标签。这有效:

<Label Grid.Row="1" Grid.Column="0" Target="{Binding ElementName=CountryCode}">
   <Label.Style>
    <Style TargetType="{x:Type Label}">
      <Style.Triggers>
          <DataTrigger Binding="{Binding ElementName=CountryCode, Path=(IsFocused)}" Value="True">
              <Setter Property="Foreground" Value="Blue" />
          </DataTrigger>
      </Style.Triggers>
    </Style>
   </Label.Style>
   <AccessText Text="{Binding Path=CountryCodeLabel}" />
</Label>
<TextBox Grid.Row="1" Grid.Column="1" Name="CountryCode" Text="{Binding Path=CountryCode}" />

但我有很多这些文本框,所以我更喜欢模板风格。这有效:

<Style x:Key="HighlightOnFocus" TargetType="{x:Type Label}">
<Style.Triggers>
   <DataTrigger Binding="{Binding ElementName=CountryCode, Path=(IsFocused)}" Value="True">
       <Setter Property="Foreground" Value="Blue" />
   </DataTrigger>
</Style.Triggers>
</Style>
...
<Label Grid.Row="1" Grid.Column="0" Style="{StaticResource HighlightOnFocus}">
   <AccessText Text="{Binding Path=CountryCodeLabel}" />
</Label>
<TextBox Grid.Row="1" Grid.Column="1" Name="CountryCode" Text="{Binding Path=CountryCode}" />

但当然我不能在那里硬编码ElementName。所以我尝试了这个:

<Style x:Key="HighlightOnFocus" TargetType="{x:Type Label}">
<Style.Triggers>
   <DataTrigger Binding="{Binding Path=(IsFocused)}" Value="True">
       <Setter Property="Foreground" Value="Blue" />
   </DataTrigger>
</Style.Triggers>
</Style>
...
<Label Grid.Row="1" Grid.Column="0" Style="{StaticResource HighlightOnFocus}" DataContext="{Binding ElementName=CountryCode}">
   <AccessText Text="{Binding Path=CountryCodeLabel}" />
</Label>
<TextBox Grid.Row="1" Grid.Column="1" Name="CountryCode" Text="{Binding Path=CountryCode}" />

但是在标签中设置DataContext会破坏我的AccessText元素中的绑定。所以问题是 - 除了设置datacontext之外,有没有办法以某种方式指定样式数据触发器的元素名称?有没有更好的方法来完成我想要做的事情?

1 个答案:

答案 0 :(得分:0)

这里有一个很好的解决方案: https://social.msdn.microsoft.com/Forums/vstudio/en-US/627e4d27-351b-4e2d-86a3-9367af3f0edf/how-to-set-a-label-style-when-the-target-textbox-has-focus?forum=wpf

一般来说,这很容易。像这样创建一个自定义附加属性:

public static class CustomAttached
{
    public static readonly DependencyProperty IsTargetFocusedProperty = DependencyProperty.RegisterAttached(
        "IsTargetFocused", typeof(bool), typeof(CustomAttached),
        new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsRender)
    );
    public static void SetIsTargetFocused(UIElement element, bool value)
    {
        element.SetValue(IsTargetFocusedProperty, value);
    }
    public static bool GetIsTargetFocused(UIElement element)
    {
        return (bool)element.GetValue(IsTargetFocusedProperty);
    }
}

然后为突出显示添加样式:

<Window.Resources>
    <Style x:Key="HighlightOnFocus" TargetType="{x:Type Label}">
        <Style.Triggers>
            <Trigger Property="local:CustomAttached.IsTargetFocused" Value="True">
                <Setter Property="Background" Value="CadetBlue" />
            </Trigger>
        </Style.Triggers>
    </Style>
</Window.Resources>

然后简单地使用它:

<Label Content="Label" Style="{StaticResource HighlightOnFocus}"
    local:CustomAttached.IsTargetFocused="{Binding IsFocused, ElementName=textBox}">
</Label>
<TextBox x:Name="textBox" Text="TextBox" />