# Select 自定义样式指南 Select 的视觉表现通过 `ControlTheme` + Design Token 系统控制。以下介绍几种常见的自定义方式。 --- ## 1. 使用属性直接控制 最简单的方式是通过 Select 的公共属性来控制外观: ```xml ``` > 📖 完整示例参考:`controlgallery/AtomUIGallery/ShowCases/Views/DataEntry/SelectShowCase.axaml` --- ## 2. 通过 Style 覆盖样式 可以在 AXAML 中使用 `Style` 对 Select 进行全局或局部样式覆盖: ### 全局统一样式 ```xml ``` ### 按模式定制 ```xml ``` ### 按尺寸定制 ```xml ``` ### 使用伪类选择器 ```xml ``` --- ## 3. 通过 ControlTheme 完全替换主题 如果需要彻底替换 Select 的模板和样式,可以定义自定义 `ControlTheme`: ```xml ``` > ⚠️ 注意:完全替换 ControlTheme 将丢失内置的 AddOnDecoratedBox 装饰、下拉动画、候选列表等功能。建议优先使用 Style 覆盖。 --- ## 4. 自定义选项渲染 通过 `OptionTemplate` 自定义下拉选项的展示方式: ```xml ``` 自定义选项类型需继承 `SelectOption`: ```csharp public record CustomOption : SelectOption { public string? Description { get; init; } public string? Emoji { get; init; } } ``` > 📖 参考:`controlgallery/AtomUIGallery/ShowCases/Views/DataEntry/SelectShowCase.axaml` 中 "Custom dropdown options" 示例。 --- ## 5. 自定义搜索过滤 通过实现 `IValueFilter` 接口自定义过滤逻辑: ```csharp public class CustomFilter : IValueFilter { public bool Filter(object? value, object? filterValue) { if (value is string valueStr && filterValue is string filterValueStr) { return valueStr.Contains(filterValueStr, StringComparison.Ordinal); } return false; } } ``` ```xml ``` 或在 code-behind 中设置自定义过滤器: ```csharp CustomSearchSelect.Filter = new CustomFilter(); ``` > 📖 参考:`controlgallery/AtomUIGallery/ShowCases/Views/DataEntry/SelectShowCase.axaml.cs` 中的 `CustomFilter` 实现。 --- ## 6. 前缀和后缀内容 通过 `ContentLeftAddOn` 和 `ContentRightAddOn` 添加内部附加内容: ```xml ``` > 📖 参考:`controlgallery/AtomUIGallery/ShowCases/Views/DataEntry/SelectShowCase.axaml` 中 "Prefix and Suffix" 示例。 --- ## 7. 控制动画行为 ```xml ``` --- ## 样式选择器速查 > 说明:Avalonia 样式选择器中使用 `atom|Select` 语法引用 `atom` XML 命名空间下的 `Select` 类型,其中 `|` 是命名空间分隔符。 ### 按模式选择 | 选择器 | 说明 | |---|---| | `atom\|Select` | 匹配所有 Select 实例 | | `atom\|Select[Mode=Single]` | 匹配单选模式 | | `atom\|Select[Mode=Multiple]` | 匹配多选模式 | | `atom\|Select[Mode=Tags]` | 匹配标签模式 | ### 按样式变体选择 | 选择器 | 说明 | |---|---| | `atom\|Select[StyleVariant=Outline]` | 匹配 Outlined 变体 | | `atom\|Select[StyleVariant=Filled]` | 匹配 Filled 变体 | | `atom\|Select[StyleVariant=Borderless]` | 匹配 Borderless 变体 | ### 按尺寸选择 | 选择器 | 说明 | |---|---| | `atom\|Select[SizeType=Large]` | 匹配大号 Select | | `atom\|Select[SizeType=Middle]` | 匹配中号 Select(默认) | | `atom\|Select[SizeType=Small]` | 匹配小号 Select | ### 按状态选择 | 选择器 | 说明 | |---|---| | `atom\|Select:dropdownopen` | 下拉面板打开状态 | | `atom\|Select:error` | 错误验证状态 | | `atom\|Select:warning` | 警告验证状态 | | `atom\|Select:pointerover` | 鼠标悬浮状态 | | `atom\|Select:disabled` | 禁用状态 | | `atom\|Select:pressed` | 按下状态 | ### 组合选择器示例 | 选择器 | 说明 | |---|---| | `atom\|Select[Mode=Single]:not(:disabled)` | 非禁用的单选 Select | | `atom\|Select[Mode=Multiple]:dropdownopen` | 多选模式且下拉打开 | | `atom\|Select[SizeType=Large][Mode=Multiple]` | 大号多选 Select | | `atom\|Select /template/ atom\|SelectAddOnDecoratedBox` | 访问模板内的装饰容器 | | `atom\|Select /template/ Border#PopupFrame` | 访问模板内的弹出框架 |