2009-10-09 57 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; 
    }