wpf - HowTo:将“Xaml 中的材料设计”“ComboBox”行为更改为仅在单击箭头时显示下拉菜单?
问题描述
您如何修改Xaml (MDIX)中的 Material Design ComboBox
,使其仅在单击箭头按钮时显示项目列表?有没有一种简单的方法可以在不破坏图书馆行为的情况下做到这一点?
您可以提供的任何见解将不胜感激。
语境
对于.NET ComboBox
, a 的默认行为IsEditable="True"
是:
- 用户单击
ComboBox
,可用选项列表不显示。<<<期望的行为 - 用户单击
ComboBox
箭头,将显示可用选项列表。
对于 MDIX ComboBox
, a 的默认行为IsEditable="True"
是:
- 用户单击
ComboBox
,将显示可用选项列表。<<<不良行为 - 用户单击
ComboBox
箭头,将显示可用选项列表。
附加SnoopWPF显示MouseLeftButtonDown
事件正在触发,但我犹豫是否要进行任何重大更改(例如制作我自己的资源副本),因为我想保持与 MDIX 的未来版本兼容。
环境
- GitHub 上的MaterialDesignInXamlToolkit项目
- 使用MaterialDesignThemes NuGet 包 v4.0.0-ci2347
解决方案
根本原因
我不知道这是否是 MDIX 库的初衷,但似乎是:
- 在
toggleButton
视觉树中高于PART_EditableTextBox
- 当您打开
toggleButton
边框时,您会看到 Button 占据了整个宽度ComboBox
结果,toggleButton
总是会拦截鼠标事件。
解决方案
我终于找到了一个解决方案:创建一个 WPF 附加属性。这使我们能够:
- 有一种更像微软的行为
ComboBox
- 同时使升级到 MDIX 库的未来版本相对容易
源代码
XAML 中的以下附加行为
<ComboBox materialDesign:TextFieldAssist.HasClearButton="True">
<!-- implementation details here -->
</ComboBox>
使用此 WPF 附加行为:
internal static class MdixComboBoxBehavior
{
public static readonly DependencyProperty UseMicrosoftBehaviorProperty = DependencyProperty.RegisterAttached(
"UseMicrosoftBehavior",
typeof(bool),
typeof(MdixComboBoxBehavior),
new UIPropertyMetadata(default(bool), UseMicrosoftBehaviorChanged));
public static bool GetUseMicrosoftBehavior(DependencyObject obj)
{
return (bool)obj.GetValue(UseMicrosoftBehaviorProperty);
}
public static void SetUseMicrosoftBehavior(DependencyObject obj, bool value)
{
obj.SetValue(UseMicrosoftBehaviorProperty, value);
}
public static void UseMicrosoftBehaviorChanged(DependencyObject s, DependencyPropertyChangedEventArgs e)
{
if (s is ComboBox comboBox)
{
comboBox.Loaded += OnControlLoaded;
}
}
private static void OnControlLoaded(object sender, RoutedEventArgs e)
{
if (sender is ComboBox comboBox)
{
if (comboBox.Template != null)
{
if (comboBox.Template.FindName("toggleButton", comboBox) is ToggleButton button)
{
// Bug fix: the following makes the MDIX ComboBox
// behave more like a Microsoft ComboBox. A list of options
// is only displayed when the user clicks the down arrow.
button.BorderThickness = new Thickness(1);
button.Width = 10;
button.HorizontalAlignment = HorizontalAlignment.Right;
}
}
}
}
}
推荐阅读
- reactjs - 如何组合多个异步文件上传,并能够同时引用和写入两个新创建的 URL 到数据库?
- loops - Fortran 循环时嵌套 Do 和 Do
- mongodb - 如何提高应用性能?[更新]
- kotlin - Thymeleaf:一个简单的 for 循环中的“异常评估 OGNL 表达式”
- reactjs - 如何在 mongodb 中使用 editor.js
- django - Django:如何使用外键的特定字段加载查询集?
- c - 即使我在 C 编程中将最小值设置为 1,Rand() 仍然会生成 0
- java - 使用 apache poi 创建的 Excel 文档中的单元格边框问题
- excel - 如何让 Excel 在显示所有组合代码中使用 VBA 代码忽略空白单元格?
- django - Django:如何计算子查询的组数?