2011-07-12 36 views
2

がフォーカスしているときにラベルの背景色を変更するトリガーを定義したいだけですが、動作しません。ボタンで同じことをすることはOKです。何か間違っていますか?私はまた、Borderとtextblockで同じ問題を抱えています。なぜIsFocusedがラベルに表示されないのですか

更新コードXAML:

<Window.Resources> 
    <SolidColorBrush x:Key="GridLineBrush" Color="#8DAED9" /> 
    <SolidColorBrush x:Key="HeaderWeekDayBackground" Color="#A5BFE1" /> 
    <Style x:Key="borderStyle" TargetType="Control"> 
     <Setter Property="Background" Value="{StaticResource HeaderWeekDayBackground}" /> 
     <Style.Triggers> 
     <Trigger Property="IsFocused" Value="true"> 
      <Setter Property="Background" Value="Blue" /> 
     </Trigger> 
     </Style.Triggers> 
    </Style> 
    </Window.Resources> 

    <Grid> 
    <Grid.RowDefinitions> 
     <RowDefinition/> 
     <RowDefinition/> 
    </Grid.RowDefinitions> 
    <Button Style="{StaticResource borderStyle}" 
     Grid.Row="0" > 
    </Button> 
    <Label Focusable="True" Style="{StaticResource borderStyle}" 
     Grid.Row="1" > 
    </Label> 
    </Grid> 
</Window> 
+0

あなたがラベルの焦点を渡そうとするにはどうすればよいですか? –

答えて

3

すべてのコントロールは、デフォルトでフォーカス可能わけではありません、それは場合に役立ちますFocusabletrueにANS参照設定。

問題は、デフォルトで、ラベルがマウスイベントからフォーカスを受け取らないことです。

フォーカスを設定するには、クリーンXAML-唯一の方法があるかどうかはわからないが、あなたは、マウスイベントを処理できます。

<Label Focusable="True" Content="Test" MouseLeftButtonUp="Label_MouseLeftButtonUp"> 
    <Label.Style> 
     <Style TargetType="Label"> 
      <Style.Triggers> 
       <Trigger Property="IsFocused" Value="True"> 
        <Setter Property="Background" Value="Red" /> 
       </Trigger> 
      </Style.Triggers> 
     </Style> 
    </Label.Style> 
</Label> 
//Note that this is not a "proper" click. 
private void Label_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) 
{ 
    var label = sender as Label; 
    label.Focus(); 
} 
+0

まだフォーカスが合っていても動作していません。 KevinBui

+0

動作しませんか? –

+0

上記のコードを参照してください、それdoesnt仕事。ラベルや枠線がマウスからフォーカスを得ることができない場合は、どうすればいいのですか? – KevinBui

関連する問題