如何解决MaterialDesign ComboBox Arrow 调整大小和更改颜色 WPF
我正在努力更改此组件,但根本无法正确完成。我试过了:
箭头目前看起来像这样,但这种行为是默认的,我想让它更大一点,所以它在大屏幕上更明显,并且还改变了箭头本身的颜色
解决方法
您可以使用 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>
另一个选项是从 here 复制默认模板并修改 Path
资源中的 MaterialDesignComboBoxToggleButton
元素。请注意,它需要您复制和粘贴相当多的 XAML。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。