问题描述
我正在努力更改此组件,但根本无法正确完成。我试过了:
箭头目前看起来像这样,但这种行为是默认的,我想让它更大一点,所以它在大屏幕上更明显,并且还改变了箭头本身的颜色
解决方法
您可以使用 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。