2009-10-09 86 views
2

我一直在尋找關於如何樣式ComboBox的以下example,但我在進入可編輯組合框時未能創建焦點效果。每當ComboBox獲得焦點時,它應進入編輯模式,並且組件應具有焦點樣式。如何將焦點樣式添加到WPF中的可編輯組合框中

的基本問題是,每當我進入編輯模式,它不是周圍ComboBox實際上具有焦點,但文本子,我一直沒能到修改文本組件上創建TriggerComboBox的邊框樣式,因爲我不知道如何從觸發器中引用父組件。

我試過在TextBox或樣式觸發器上加ControlTemplateTrigger。我試圖通過名稱或使用TemplateBinding選項來參考ComboBox,但沒有任何運氣。一個簡單的例子將非常感激。

回答

3

綁定IsKeyboardFocusWithin到IsDropDownOpen

<ComboBox ItemsSource="{Binding SortedItems}" 
      StaysOpenOnEdit="True" 
      IsDropDownOpen="{Binding IsKeyboardFocusWithin, RelativeSource={RelativeSource Self}, Mode=OneWay}" /> 
1
private void cmbSpecialHandling_GotFocus(object sender, RoutedEventArgs e) 
     { 
      Thickness th = new Thickness(2); 
      cmbSpecialHandling.BorderThickness = th; 
      cmbSpecialHandling.BorderBrush = this.FindResource("TabFocusColor") as SolidColorBrush; 
     } 

     private void cmbSpecialHandling_GotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e) 
     { 
      Thickness th = new Thickness(2); 
      cmbSpecialHandling.BorderThickness = th; 
      cmbSpecialHandling.BorderBrush = this.FindResource("TabFocusColor") as SolidColorBrush; 
     } 

     private void cmbSpecialHandling_LostFocus(object sender, RoutedEventArgs e) 
     { 
      cmbSpecialHandling.BorderBrush = Brushes.Transparent; 
     } 
1

設置組合框的邊框刷在其Gotfocus並使它在失去焦點透明:

private void comboBox_GotFocus(object sender, RoutedEventArgs e) 
    { 
     Thickness th = new Thickness(2); 
     comboBox.BorderThickness = th; 
     comboBox.BorderBrush = this.FindResource("TabFocusColor") as SolidColorBrush; 
        or 
    comboBox.BorderBrush = Brushes.Green; 
    } 


    private void comboBox_LostFocus(object sender, RoutedEventArgs e) 
    { 
     comboBox.BorderBrush = Brushes.Transparent; 
    }