2011-03-28 66 views
2

我想突出顯示標籤,當關聯的文本框有焦點。這個作品:WPF:如何在目標文本框具有焦點時設置標籤樣式?

<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元素中的綁定。所以問題是 - 是否有一種方式以某種方式指定樣式datatrigger的元素名稱,而不是設置datacontext?有沒有更好的方法來完成我想要做的事情?

回答

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" /> 
+0

我不不知道爲什麼語法突出顯示不起作用,即使我手動添加了標記。無論如何,解決方案工作:) – Roemer 2017-03-09 08:27:30

相關問題