如何将焦点样式添加到WPF中的可编辑ComboBox

时间:2009-10-09 13:37:19

标签: wpf xaml combobox styling

我一直在查看以下example关于如何设置ComboBox的样式,但是在进入可编辑的组合框时我无法创建焦点效果。每当ComboBox获得焦点时,它应进入编辑模式,组件应具有焦点样式。

基本问题是每当我进入编辑模式时,实际上没有焦点的周围ComboBox,但文本子组件和我无法创建Trigger修改ComboBox边框样式的文本组件,因为我不知道如何从触发器引用父组件。

我尝试在ControlTemplate或样式触发器上添加Trigger TextBox。我试图通过名称或使用ComboBox选项来引用TemplateBinding,但没有任何运气。一个简单的例子将非常感激。

3 个答案:

答案 0 :(得分:3)

将IsKeyboardFocusWithin绑定到IsDropDownOpen

<ComboBox ItemsSource="{Binding SortedItems}"
          StaysOpenOnEdit="True"
          IsDropDownOpen="{Binding IsKeyboardFocusWithin, RelativeSource={RelativeSource Self}, Mode=OneWay}" />

答案 1 :(得分: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;
        }

答案 2 :(得分: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;
    }