如何向 WPF 中的可编辑组合框添加焦点样式

发布于 2024-08-07 03:40:16 字数 579 浏览 2 评论 0原文

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

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

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

I've been looking at the following example on how to style the ComboBox, but I haven't been able to create a focus effect when going into an editable combo box. Whenever the ComboBox receives focus, it should go into edit mode and the component should have a focus style.

The basic problem is that whenever I go into the edit mode, it's not the surrounding ComboBox which actually has the focus, but the text subcomponent and I haven't been able to create a Trigger on the text component which modifies the ComboBox's border style since I don't know how to refer to the parent component from the trigger.

I've tried adding ControlTemplate Trigger on the TextBox, or style trigger. I've tried to refer to the ComboBox by name or by using the TemplateBinding option, but without any luck. A simple example would be very appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

岁月静好 2024-08-14 03:40:16

将 IsKeyboardFocusWithin 绑定到 IsDropDownOpen

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

Bind IsKeyboardFocusWithin to IsDropDownOpen

<ComboBox ItemsSource="{Binding SortedItems}"
          StaysOpenOnEdit="True"
          IsDropDownOpen="{Binding IsKeyboardFocusWithin, RelativeSource={RelativeSource Self}, Mode=OneWay}" />
你又不是我 2024-08-14 03:40:16
  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;
        }
  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;
        }
咽泪装欢 2024-08-14 03:40:16

在其 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;
    }

Set the border brush of combobox in its Gotfocus and make it transparent in lost focus:

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;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文