2014-09-02 73 views
1

我有什麼一些可能看起來像一個簡單的問題,儘管我盡了最大努力,我一直無法「修復」這樣的問題:TextBox控件以及自定義樣式不點火的GotFocus

我有一個自定義樣式的文本框,在「通用」程序中定義;此樣式將覆蓋默認值,因此每次有人使用TextBox時都會使用該樣式。 問題是應用該樣式時,在GotFocus,IsKeyboardFocusWithinChanged在和PreviewGotKeyboardFocus事件-not-解僱任何更長的時間。 我已經驗證過,只有在應用此風格時纔會發生這種情況(如果我將其註釋掉並運行應用程序,則會正確觸發事件)。

所以我的問題基本上是,任何人都經歷了類似的話?如果是這樣,有沒有人知道這個問題的解決方案?

樣式如下(靜態資源的簡單SolidColorBrushes):

<Style TargetType="{x:Type TextBox}" x:Key="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}"> 
    <Setter Property="FocusVisualStyle" Value="{x:Null}" /> 
    <Setter Property="OverridesDefaultStyle" Value="True" /> 
    <Setter Property="BorderBrush" Value="{StaticResource BrushBorder}"/> 
    <Setter Property="ContextMenu" Value="{StaticResource TextBoxContextMenu}" /> 
    <Setter Property="Foreground" Value="{StaticResource BrushForeground}" /> 
    <Setter Property="SelectionBrush" Value="{StaticResource BrushHighlight}" /> 
    <Setter Property="Background" Value="{StaticResource BrushBackground}"/> 
    <Setter Property="KeyboardNavigation.TabNavigation" Value="None"/> 
    <Setter Property="SnapsToDevicePixels" Value="True"/> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type TextBox}"> 
       <Border BorderThickness="1" x:Name="Border" BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}"> 
        <ScrollViewer x:Name="PART_ContentHost" 
            Margin="0" /> 
       </Border> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
    <Style.Triggers> 
     <Trigger Property="IsKeyboardFocusWithin" Value="True"> 
      <Setter Property="Background" Value="{StaticResource BrushLightBg}" /> 
     </Trigger> 
     <Trigger Property="IsMouseOver" Value="True"> 
      <Setter Property="Background" Value="{StaticResource BrushMouseoverBackground}" /> 
     </Trigger> 
    </Style.Triggers> 
</Style> 

回答

2

嗨THI工作就好在這裏,我不得不添加一些顏色,你人失蹤,這將讓你的眼睛流血赦免這一點,但我在這裏有點倉促:)的是

<!-- nice colors you were missing them ;) --> 
<SolidColorBrush x:Key="BrushBorder" Color="Blue"/> 
<SolidColorBrush x:Key="TextBoxContextMenu" Color="DeepPink"/> 
<SolidColorBrush x:Key="BrushForeground" Color="Red"/>   
<SolidColorBrush x:Key="BrushHighlight" Color="Aqua"/> 
<SolidColorBrush x:Key="BrushBackground" Color="DarkBlue"/> 
<SolidColorBrush x:Key="BrushLightBg" Color="LightSeaGreen"/> 
<SolidColorBrush x:Key="BrushMouseoverBackground" Color="Yellow"/> 

<Style TargetType="{x:Type TextBox}" x:Key="TextBoxStyleMessed" BasedOn="{StaticResource {x:Type TextBox}}" > 
    <Setter Property="FocusVisualStyle" Value="{x:Null}" /> 
    <Setter Property="OverridesDefaultStyle" Value="True" /> 
    <Setter Property="BorderBrush" Value="{StaticResource BrushBorder}"/> 
    <!--<Setter Property="ContextMenu" Value="{StaticResource TextBoxContextMenu}" />--> 
    <Setter Property="Foreground" Value="{StaticResource BrushForeground}" /> 
    <Setter Property="SelectionBrush" Value="{StaticResource BrushHighlight}" /> 
    <Setter Property="Background" Value="{StaticResource BrushBackground}"/> 
    <Setter Property="KeyboardNavigation.TabNavigation" Value="None"/> 
    <Setter Property="SnapsToDevicePixels" Value="True"/> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type TextBox}"> 
       <Border BorderThickness="1" x:Name="Border" BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}"> 
        <ScrollViewer x:Name="PART_ContentHost" Margin="0" /> 
       </Border> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
    <Style.Triggers> 
     <Trigger Property="IsKeyboardFocusWithin" Value="True"> 
      <Setter Property="Background" Value="{StaticResource BrushLightBg}" /> 
     </Trigger> 
     <Trigger Property="IsMouseOver" Value="True"> 
      <Setter Property="Background" Value="{StaticResource BrushMouseoverBackground}" /> 
     </Trigger> 
    </Style.Triggers> 
</Style> 

..... 

<Grid> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="Auto"/> 
     <RowDefinition Height="*"/> 
    </Grid.RowDefinitions> 
    <TextBox Text="Hello!" HorizontalAlignment="Center" VerticalAlignment="Center" Style="{DynamicResource TextBoxStyleMessed}" 
      x:Name="HateThisNonMVVMStuff"/> 
    <Button Grid.Row="1">Hodwy</Button> 
</Grid> 

不得不削減一些角落,但討厭的隱藏代碼:

public partial class MainWindow : Window 
{ 
    public MainWindow() 
    { 
     InitializeComponent(); 
     Loaded += MainWindow_Loaded; 
    } 

    void MainWindow_Loaded(object sender, RoutedEventArgs e) 
    { 
     HateThisNonMVVMStuff.PreviewLostKeyboardFocus += HateThisNonMVVMStuff_PreviewLostKeyboardFocus; 
     HateThisNonMVVMStuff.LostFocus += UIElement_OnLostFocus; 
     HateThisNonMVVMStuff.GotFocus += UIElement_OnGotFocus; 

    } 

    void HateThisNonMVVMStuff_PreviewLostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e) 
    { 
     System.Diagnostics.Trace.WriteLine("PrevLostFocus!"); 
    } 

    private void UIElement_OnGotFocus(object sender, RoutedEventArgs e) 
    { 
     System.Diagnostics.Trace.WriteLine("Hei! Gained focus"); 
    } 

    private void UIElement_OnLostFocus(object sender, RoutedEventArgs e) 
    { 
     System.Diagnostics.Trace.WriteLine("Hei! Lost focus");   
    } 
} 

事件隨着你的風格而發生,我只是將它用於這一個盒子的風格。我在你的contextmenu上遇到了一個異常,並且它不會有我討厭的顏色,但是,嘿,誰會! :d

希望它能幫助,

乾杯,

了Stian

+0

所以你-are-得到我所提到的焦點事件?你到底在哪裏爲這些添加處理程序?我通過單獨的行爲類來添加它們,並且它們在沒有樣式的情況下工作得很好 - 但不符合樣式。 – t0yk4t 2014-09-02 12:26:33

+0

@MelanieSchmidt將它們添加到那裏的惡意代碼中,不得不削減索姆角。但他們會在這裏開火。忘記將其粘貼在 – 2014-09-02 12:28:32

+1

好吧,這真的很奇怪 - 他們現在的工作,我也...肯定只有一個僥倖...標記你的答案作爲解決方案,感謝您的時間! – t0yk4t 2014-09-02 12:31:42

相關問題