我必须改变标准选择器控件的外观。目前仅适用于 Windows。所以我做了 AppendToMapping 并成功更改了背景颜色。
MauiProgram.cs中的代码:
Microsoft.Maui.Handlers.ElementHandler.ElementMapper.AppendToMapping("Classic", (handler, view) =>
{
if (view is myPicker)
{
EntryMapper.Map(handler, view);
}
});
和我的新控制定义:
public class myPicker : Picker
{
public static BindableProperty PopupBackgroundColorProperty = BindableProperty.Create(nameof(PopupBackgroundColor), typeof(Color), typeof(iCPicker), Color.FromRgba(0, 0, 0, 0));
public Color PopupBackgroundColor
{
get => (Color)GetValue(PopupBackgroundColorProperty);
set => SetValue(PopupBackgroundColorProperty, value);
}
}
XAML声明:
<uiUtilities:myPicker PopupBackgroundColor="Blue"/>
一切都很好设置 - 用于测试 - 组合框的背景颜色
public static class EntryMapper
{
public static void Map(IElementHandler handler, IElement view)
{
if(view is myPicker)
{
var casted = (PickerHandler)handler;
var view2 = (myPicker)view;
view2.BackgroundColor = view2.PopupBackgroundColor; // this works for test
// but actually want to change the dropdown of this combobox
}
}
}
但我的目标是修改当您单击选择器/组合框时出现的下拉列表。我想设置它的背景颜色、角半径、阴影、边框,也许稍微移动它,也许自定义更多。如果可能,请为当前选定的项目禁用此窗口的主题颜色。
我在 WinUI 3.0 中查看了组合框的结构,因为这是 Windows 的底层控件,但无论如何我无法通过 view2 对象找到它。我试图挖掘 VisualTree 等
如何访问这个下拉元素?
我也试过通过 Handlers 修改下拉元素,但像你一样失败了。
但我的目标是修改当您单击选择器/组合框时出现的下拉列表。我想设置它的背景颜色、角半径、阴影、边框,也许稍微移动它,也许自定义更多。如果可能,请为当前选定的项目禁用此窗口的主题颜色。
可以参考windows.ui.xaml.controls.combobox.
的源码以下拉框的背景和边框粗细为例,在Project/Platforms/Windows/App.xaml文件中添加一些代码:
<maui:MauiWinUIApplication
x:Class="MauiApp1.WinUI.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:maui="using:Microsoft.Maui"
xmlns:local="using:MauiApp1.WinUI">
<maui:MauiWinUIApplication.Resources>
<SolidColorBrush x:Key="ComboBoxDropDownBackground" Color="#48b7d5" />
<Thickness x:Key="ComboBoxDropdownBorderThickness">5</Thickness>
</maui:MauiWinUIApplication.Resources>
</maui:MauiWinUIApplication>
这就是效果。