admin 管理员组

文章数量: 1184232

My markup does not want to change the background of neither the ComboBox nor for the selected ComboBoxItem. It additionally does not change the hover color of ComboBoxItems. How does this make sense? Also if possible, I'd also like to know how to remove the scroll bar in the list with the items, since I couldn't find anything about it.

Preview of Issue

<UserControl.Resources>
    <Style TargetType="{x:Type ComboBox}">
        <Setter Property="Background" Value="{DynamicResource PanelBackground}" />
        <Setter Property="Foreground" Value="{DynamicResource TextColor}" />
        <Setter Property="BorderBrush" Value="{DynamicResource BorderBrush}" />
    </Style>
    <Style TargetType="{x:Type ComboBoxItem}">
        <Setter Property="Background" Value="{DynamicResource PanelBackground}" />
        <Setter Property="Foreground" Value="{DynamicResource TextColor}" />
        <Setter Property="BorderBrush" Value="{DynamicResource BorderBrush}" />
        <Setter Property="Padding" Value="4,2" />
        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="Background" Value="{DynamicResource HoverColor}" />
            </Trigger>
            <Trigger Property="IsSelected" Value="True">
                <Setter Property="Background" Value="{DynamicResource PanelBackground}" />
                <Setter Property="Foreground" Value="{DynamicResource TextColor}" />
            </Trigger>
        </Style.Triggers>
    </Style>

I've tried with and without control templates and I have even played around with properties of different values and other, seemingly unrelated, properties. I'm using a control template for the hover color on a button, but either I wrote the markup incorrectly or it doesn't work with ComboBoxes.

My markup does not want to change the background of neither the ComboBox nor for the selected ComboBoxItem. It additionally does not change the hover color of ComboBoxItems. How does this make sense? Also if possible, I'd also like to know how to remove the scroll bar in the list with the items, since I couldn't find anything about it.

Preview of Issue

<UserControl.Resources>
    <Style TargetType="{x:Type ComboBox}">
        <Setter Property="Background" Value="{DynamicResource PanelBackground}" />
        <Setter Property="Foreground" Value="{DynamicResource TextColor}" />
        <Setter Property="BorderBrush" Value="{DynamicResource BorderBrush}" />
    </Style>
    <Style TargetType="{x:Type ComboBoxItem}">
        <Setter Property="Background" Value="{DynamicResource PanelBackground}" />
        <Setter Property="Foreground" Value="{DynamicResource TextColor}" />
        <Setter Property="BorderBrush" Value="{DynamicResource BorderBrush}" />
        <Setter Property="Padding" Value="4,2" />
        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="Background" Value="{DynamicResource HoverColor}" />
            </Trigger>
            <Trigger Property="IsSelected" Value="True">
                <Setter Property="Background" Value="{DynamicResource PanelBackground}" />
                <Setter Property="Foreground" Value="{DynamicResource TextColor}" />
            </Trigger>
        </Style.Triggers>
    </Style>

I've tried with and without control templates and I have even played around with properties of different values and other, seemingly unrelated, properties. I'm using a control template for the hover color on a button, but either I wrote the markup incorrectly or it doesn't work with ComboBoxes.

Share Improve this question edited Feb 12 at 10:31 ASh 35.7k9 gold badges65 silver badges87 bronze badges asked Feb 12 at 10:16 wildREAwildREA 11 bronze badge 3
  • Please see my comment to this answer. A lot of my experience and experience of many developers demonstrated that the root of the problem is trying to utilize existing ComboBox control. Note that control template is not the only solution; I would prefer a custom control. – Sergey A Kryukov Commented Feb 12 at 21:55
  • @SergeyAKryukov Out of curiosity, what is the difference between a default ComboBox with custom Style and ControlTemplate, and a custom control which you are refering to? Generally, just to change the apperance, the former should suffice. – emoacht Commented Feb 13 at 12:05
  • @emoacht — “what is the difference between” I could answer in details, but it would take a lot more time. I would place a formal answer after I develop some sample. Big difference. Suppose you already know about control template. The other approach is similar to the ComboBox. You create a new class public class ComboBox : System.Windows.Controls.Primitives.Selector, and so on. Basically, from scratch. Inside the control, use elements for text, list, and button(s). Define your behavior. Define all dependency properties you may need for the new flexibility. Reuse in different projects. – Sergey A Kryukov Commented Feb 13 at 14:27
Add a comment  | 

1 Answer 1

Reset to default 0

for your usecase it seems like you have to generate a custom control template.

For reference here someone already tried to create a custom combobox. custom combobox in wpf Application

If you'd like to use the original design, you can find the source here: https://learn.microsoft/en-us/dotnet/desktop/wpf/controls/combobox-styles-and-templates?view=netframeworkdesktop-4.8#combobox-controltemplate-example

Hope I was able to help you with those sources for now

PS: in the complete template you also find a setter for the ScrollBarVisibility, which value is in default Auto

本文标签:

Error[2]: Invalid argument supplied for foreach(), File: /www/wwwroot/roclinux.cn/tmp/view_template_quzhiwa_htm_read.htm, Line: 58
File: /www/wwwroot/roclinux.cn/tmp/route_read.php, Line: 205, include(/www/wwwroot/roclinux.cn/tmp/view_template_quzhiwa_htm_read.htm)
File: /www/wwwroot/roclinux.cn/tmp/index.inc.php, Line: 129, include(/www/wwwroot/roclinux.cn/tmp/route_read.php)
File: /www/wwwroot/roclinux.cn/index.php, Line: 29, include(/www/wwwroot/roclinux.cn/tmp/index.inc.php)