【问题标题】:MaterialDesign ComboBox Arrow Resize And Change Color WPFMaterialDesign ComboBox 箭头调整大小和更改颜色 WPF
【发布时间】:2021-05-13 22:11:26
【问题描述】:

我正在努力更改此组件,但根本无法做到这一点。我试过了:

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

箭头目前看起来像这样,但这是默认行为,我想让它更大一点,以便在大屏幕上更明显,同时改变箭头本身的颜色

【问题讨论】:

    标签: c# wpf combobox material-design-in-xaml


    【解决方案1】:

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

    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>
    

    结果:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-12-14
      • 2015-11-24
      • 1970-01-01
      • 2023-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多