2011-09-29 139 views
0

我有一個UserControl子類,它包含一個Grid,又包含了幾個TextBlock S和Border(其中也包含了TextBlock)。見下面的代碼。WPF用戶控件焦點

<UserControl x:Class="ProjectNS.MyUserControl" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     xmlns:my="clr-namespace:ProjectNS" 
     mc:Ignorable="d" 
     Height="49" Width="150" BottomResizeLocked="True" TopResizeLocked="True" 
     MoveLocks="Vertical" Margin="0,-4" Focusable="True"> 
     <my:MyUserControl.Resources> 
      <Style x:Key="BorderStyle" TargetType="Border"> 
       <Setter Property="Background" Value="Blue"/> 
       <Setter Property="BorderBrush" Value="Blue"/> 
       <Style.Triggers> 
        <DataTrigger Binding="{Binding RelativeSource={RelativeSource 
        AncestorType={x:Type my:GanttBar}}, Path=IsKeyboardFocusWithin}" 
        Value="True"> 
         <Setter Property="Background" Value="{StaticResource 
         SelectedGanttBarBackGroundBrush}"/> 
         <Setter Property="BorderBrush" Value="{StaticResource 
         SelectedGanttBarBorderBrush}"/> 
        </DataTrigger> 
       </Style.Triggers> 
      </Style> 
     </my:MyUserControl.Resources> 
     <Grid> 
      <Grid.RowDefinitions> 
       <RowDefinition Height="2*" /> 
       <RowDefinition Height="3*" /> 
       <RowDefinition Height="2*" /> 
      </Grid.RowDefinitions> 
      <Label FontSize="8.5" HorizontalAlignment="Left" 
      VerticalAlignment="Bottom" Margin="4,0,0,0" Foreground="Green" /> 
      <Border Grid.Row="1" CornerRadius="5" BorderThickness="1.5" Style=" 
      {StaticResource BorderStyle}" FocusVisualStyle="{StaticResource 
      SelectedBorderStyle}" Focusable="True" > 
       <Label FontSize="10" HorizontalAlignment="Center" FontWeight="Bold" 
       VerticalAlignment="Top" Foreground="White" Margin="0,2,0,0" /> 
      </Border> 
      <Label HorizontalAlignment="Right" FontSize="8.5" Grid.Row="2" 
      VerticalAlignment="Top" Margin="0,0,4,0" Foreground="Red" /> 
     </Grid> 
    </my:MyUserControl> 

我試圖讓嵌入邊框的顏色時,我UserControl接收焦點改變顏色,但我不能爲我的生命搞清楚什麼實際收到時,我點擊控制重點。我已經嘗試過使用每個控件的GotFocus事件,並且程序運行後沒有任何事件觸發。

回答

1

在屬性IsKeyboardFocusWithin上使用DataTrigger。我不確切的語法,但它應該看起來像這樣:

<Style TargetType="{x:Type Border}"> 
    <Setter Property="BorderBrush" Value="Red" /> 
    <Style.Triggers> 

     <!-- Will change Border color when KeyboardFocus inside Border --> 
     <Trigger Property="IsKeyboardFocusWithin" Value="True"> 
      <Setter Property="BorderBrush" Value="Green" /> 
     </Trigger> 

     <!-- Will change Border color when UserControl contains KeyboardFocus --> 
     <DataTrigger Binding="{Binding RelativeSource={RelativeSource 
      AncestorType={x:Type local:MyUserControl}}, 
      Path=IsKeyboardFocusWithin}" Value="True"> 
      <Setter Property="BorderBrush" Value="Blue" /> 
     </DataTrigger> 

    </Style.Triggers> 
</Style> 
+0

我甚至不知道什麼是焦點(UserControl',任何'TextBlock's或' Border')。如果「邊框」變得越來越重要,那麼我會同意這應該起作用。如果'Border'沒有得到關注,那麼當UserControl'中的其他內容獲得焦點時,如何改變它的樣式? – gregsdennis

+0

@kungfumath如果焦點位於邊框內的ANY控件內,IsKeyboardFocusWithin將返回true。我並不是100%確定邊界具有該屬性,但如果不是,您可以使用'RelativeSource'綁定來查看UserControl.IsKeyboardFocusWithin是否爲true,如果UserControl中的ANY控件具有焦點,則返回true。 – Rachel

+0

@kungfumath我將兩個綁定都添加到了我的答案中,並對兩者進行了測試以確保它們可以正常工作 – Rachel