微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

MaterialDesign ComboBox Arrow 调整大小和更改颜色 WPF

如何解决MaterialDesign ComboBox Arrow 调整大小和更改颜色 WPF

我正在努力更改此组件,但根本无法正确完成。我试过了:

  1. 使用 WPF 功能
    1. “样式 → 转换为新资源..” - 代码导致空白组合框
    2. “模板 → 转换为新资源..” - 导致奇怪的 ComboBox 看起来像认的非 MaterialDesign 一个
  2. this 复制到我的资源字典并尝试对其进行编辑,但它需要大量转换器和其他一些我无法使其正常工作的东西

箭头目前看起来像这样,但这种行为是认的,我想让它更大一点,所以它在大屏幕上更明显,并且还改变了箭头本身的颜色

it currently looks like this,but I want to make it a bit bigger,so it's more visible on big screen and also change the color

解决方法

您可以使用 VisualTreeHelper 类获取对默认模板中 Path 中的 ToggleButton 的引用,然后设置其属性:

private void ComboBox_Loaded(object sender,RoutedEventArgs e)
{
    ToggleButton toggleButton = FindVisualChild<ToggleButton>((ComboBox)sender);
    if (toggleButton != null)
    {
        Path path = FindVisualChild<Path>(toggleButton);
        if (path != null)
        {
            path.Width = 20;
            path.Height = 20;
            path.Fill = Brushes.Red;
        }
    }

}

private static T FindVisualChild<T>(Visual visual) where T : Visual
{
    for (int i = 0; i < VisualTreeHelper.GetChildrenCount(visual); i++)
    {
        Visual child = (Visual)VisualTreeHelper.GetChild(visual,i);
        if (child != null)
        {
            T correctlyTyped = child as T;
            if (correctlyTyped != null)
                return correctlyTyped;

            T descendent = FindVisualChild<T>(child);
            if (descendent != null)
                return descendent;
        }
    }
    return null;
}

示例 XAML 标记:

<ComboBox Margin="100" Loaded="ComboBox_Loaded">
    <ComboBoxItem>1</ComboBoxItem>
    <ComboBoxItem>2</ComboBoxItem>
    <ComboBoxItem>3</ComboBoxItem>
</ComboBox>

结果: enter image description here

另一个选项是从 here 复制默认模板并修改 Path 资源中的 MaterialDesignComboBoxToggleButton 元素。请注意,它需要您复制和粘贴相当多的 XAML。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。