MaterialDesign ComboBox 箭头调整大小和更改颜色 WPF
MaterialDesign ComboBox Arrow Resize And Change Color WPF
我正在努力更改此组件,但根本无法正确完成。我试过:
- 使用 WPF 功能
- “样式 → 转换为新资源..”- 根据默认代码生成空白组合框
- “模板 → 转换为新资源..”- 导致奇怪的 ComboBox 看起来像默认的非 MaterialDesign 一样
- 正在将 this 复制到我的资源字典并尝试对其进行编辑,但它需要大量的转换器和其他我无法使其正常工作的东西
箭头目前看起来像这样,但这是默认行为,我想让它更大一点,以便在大屏幕上更明显,同时更改箭头本身的颜色
您可以使用 VisualTreeHelper
class 在默认模板中获取对 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。
我正在努力更改此组件,但根本无法正确完成。我试过:
- 使用 WPF 功能
- “样式 → 转换为新资源..”- 根据默认代码生成空白组合框
- “模板 → 转换为新资源..”- 导致奇怪的 ComboBox 看起来像默认的非 MaterialDesign 一样
- 正在将 this 复制到我的资源字典并尝试对其进行编辑,但它需要大量的转换器和其他我无法使其正常工作的东西
箭头目前看起来像这样,但这是默认行为,我想让它更大一点,以便在大屏幕上更明显,同时更改箭头本身的颜色
您可以使用 VisualTreeHelper
class 在默认模板中获取对 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。