Avalonia实战实例三:实现可输入框的ComboBox控件

news2025/3/2 4:01:16

文章目录

  • 一、Avalonia中的ComboBox控件
  • 二、更改Template,并添加水印


接着上篇关闭按钮实现登录界面
实现一个可输入,可下拉的用户名输入框
在这里插入图片描述

一、Avalonia中的ComboBox控件

Avalonia中Fluent主题里ComboBox实现:

<ControlTheme x:Key="{x:Type ComboBox}" TargetType="ComboBox">
  <Setter Property="Padding" Value="{DynamicResource ComboBoxPadding}" />
  <Setter Property="FocusAdorner" Value="{x:Null}" />
  <Setter Property="MaxDropDownHeight" Value="504" />
  <Setter Property="Foreground" Value="{DynamicResource ComboBoxForeground}" />
  <Setter Property="Background" Value="{DynamicResource ComboBoxBackground}" />
  <Setter Property="BorderBrush" Value="{DynamicResource ComboBoxBorderBrush}" />
  <Setter Property="BorderThickness" Value="{DynamicResource ComboBoxBorderThemeThickness}" />
  <Setter Property="CornerRadius" Value="{DynamicResource ControlCornerRadius}" />
  <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Disabled" />
  <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto" />
  <Setter Property="MinHeight" Value="{DynamicResource ComboBoxMinHeight}" />
  <Setter Property="HorizontalContentAlignment" Value="Stretch" />
  <Setter Property="VerticalContentAlignment" Value="Center" />
  <Setter Property="HorizontalAlignment" Value="Left" />
  <Setter Property="VerticalAlignment" Value="Top" />
  <Setter Property="PlaceholderForeground" Value="{DynamicResource ComboBoxPlaceHolderForeground}" />
  <Setter Property="Template">
    <ControlTemplate>
      <DataValidationErrors>
        <Grid ColumnDefinitions="*,32">
          <Border x:Name="Background"
                  Grid.Column="0"
                  Grid.ColumnSpan="2"
                  Background="{TemplateBinding Background}"
                  BorderBrush="{TemplateBinding BorderBrush}"
                  BorderThickness="{TemplateBinding BorderThickness}"
                  CornerRadius="{TemplateBinding CornerRadius}"
                  MinWidth="{DynamicResource ComboBoxThemeMinWidth}" />
          <Border x:Name="HighlightBackground"
                  Grid.Column="0"
                  Grid.ColumnSpan="2"
                  Background="{DynamicResource ComboBoxBackgroundUnfocused}"
                  BorderBrush="{DynamicResource ComboBoxBackgroundBorderBrushUnfocused}"
                  BorderThickness="{TemplateBinding BorderThickness}"
                  CornerRadius="{TemplateBinding CornerRadius}"
                  IsVisible="False"/>
          <TextBlock x:Name="PlaceholderTextBlock"
                     Grid.Column="0"
                     HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                     VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                     Margin="{TemplateBinding Padding}"
                     Text="{TemplateBinding PlaceholderText}"
                     Foreground="{TemplateBinding PlaceholderForeground}"
                     IsVisible="{TemplateBinding SelectionBoxItem, Converter={x:Static ObjectConverters.IsNull}}" />
          <ContentControl x:Name="ContentPresenter"
                          Content="{TemplateBinding SelectionBoxItem}"
                          Grid.Column="0"
                          Margin="{TemplateBinding Padding}"
                          HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
                          ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}"
                          VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}">
          </ContentControl>

          <Border x:Name="DropDownOverlay"
                  Grid.Column="1"
                  Background="Transparent"
                  Margin="0,1,1,1"
                  Width="30"
                  IsVisible="False"
                  HorizontalAlignment="Right" />

          <PathIcon x:Name="DropDownGlyph"
                    Grid.Column="1"
                    UseLayoutRounding="False"
                    IsHitTestVisible="False"
                    Height="12"
                    Width="12"
                    Margin="0,0,10,0"
                    HorizontalAlignment="Right"
                    VerticalAlignment="Center"
                    Foreground="{DynamicResource ComboBoxDropDownGlyphForeground}"
                    Data="M1939 486L2029 576L1024 1581L19 576L109 486L1024 1401L1939 486Z"/>

          <Popup Name="PART_Popup"
                 WindowManagerAddShadowHint="False"
                 IsOpen="{TemplateBinding IsDropDownOpen, Mode=TwoWay}"
                 MinWidth="{Binding Bounds.Width, RelativeSource={RelativeSource TemplatedParent}}"
                 MaxHeight="{TemplateBinding MaxDropDownHeight}"
                 PlacementTarget="Background"
                 IsLightDismissEnabled="True"
                 InheritsTransform="True">
            <Border x:Name="PopupBorder"
                    Background="{DynamicResource ComboBoxDropDownBackground}"
                    BorderBrush="{DynamicResource ComboBoxDropDownBorderBrush}"
                    BorderThickness="{DynamicResource ComboBoxDropdownBorderThickness}"
                    Padding="{DynamicResource ComboBoxDropdownBorderPadding}"
                    HorizontalAlignment="Stretch"
                    CornerRadius="{DynamicResource OverlayCornerRadius}">
              <ScrollViewer HorizontalScrollBarVisibility="{TemplateBinding ScrollViewer.HorizontalScrollBarVisibility}"
                            VerticalScrollBarVisibility="{TemplateBinding ScrollViewer.VerticalScrollBarVisibility}"
                            IsDeferredScrollingEnabled="{TemplateBinding (ScrollViewer.IsDeferredScrollingEnabled)}">
                <ItemsPresenter Name="PART_ItemsPresenter"
                                Margin="{DynamicResource ComboBoxDropdownContentMargin}"
                                ItemsPanel="{TemplateBinding ItemsPanel}" />
              </ScrollViewer>
            </Border>
          </Popup>
        </Grid>
      </DataValidationErrors>
    </ControlTemplate>
  </Setter>

  <!--  PointerOver State  -->
  <Style Selector="^:pointerover /template/ Border#Background">
    <Setter Property="Background" Value="{DynamicResource ComboBoxBackgroundPointerOver}" />
    <Setter Property="BorderBrush" Value="{DynamicResource ComboBoxBorderBrushPointerOver}" />
  </Style>

  <!--  Pressed State  -->
  <Style Selector="^:pressed /template/ Border#Background">
    <Setter Property="Background" Value="{DynamicResource ComboBoxBackgroundPressed}" />
    <Setter Property="BorderBrush" Value="{DynamicResource ComboBoxBorderBrushPressed}" />
  </Style>

  <!-- Error State -->
  <Style Selector="^:error /template/ Border#Background">
    <Setter Property="BorderBrush" Value="{DynamicResource SystemControlErrorTextForegroundBrush}" />
  </Style>

  <!--  Focus Pressed State  -->
  <Style Selector="^:focused:pressed">
    <Style Selector="^ /template/ ContentControl#ContentPresenter">
      <Setter Property="Foreground" Value="{DynamicResource ComboBoxForegroundFocusedPressed}" />
    </Style>
    <Style Selector="^ /template/ TextBlock#PlaceholderTextBlock">
      <Setter Property="Foreground" Value="{DynamicResource ComboBoxPlaceHolderForegroundFocusedPressed}" />
    </Style>
    <Style Selector="^ /template/ PathIcon#DropDownGlyph">
      <Setter Property="Foreground" Value="{DynamicResource ComboBoxDropDownGlyphForegroundFocusedPressed}" />
    </Style>
  </Style>

  <!--  Focused State  -->
  <Style Selector="^:focus-visible">
    <Style Selector="^ /template/ Border#HighlightBackground">
      <Setter Property="IsVisible" Value="True" />
      <Setter Property="BorderBrush" Value="{DynamicResource ComboBoxBackgroundBorderBrushFocused}" />
    </Style>
    <Style Selector="^ /template/ ContentControl#ContentPresenter">
      <Setter Property="Foreground" Value="{DynamicResource ComboBoxForegroundFocused}" />
    </Style>
    <Style Selector="^ /template/ TextBlock#PlaceholderTextBlock">
      <Setter Property="Foreground" Value="{DynamicResource ComboBoxForegroundFocused}" />
    </Style>
    <Style Selector="^ /template/ PathIcon#DropDownGlyph">
      <Setter Property="Foreground" Value="{DynamicResource ComboBoxDropDownGlyphForegroundFocused}" />
    </Style>
  </Style>

  <!--  Disabled State  -->
  <Style Selector="^:disabled">
    <Style Selector="^ /template/ Border#Background">
      <Setter Property="Background" Value="{DynamicResource ComboBoxBackgroundDisabled}" />
      <Setter Property="BorderBrush" Value="{DynamicResource ComboBoxBorderBrushDisabled}" />
    </Style>
    <Style Selector="^ /template/ ContentControl#ContentPresenter">
      <Setter Property="Foreground" Value="{DynamicResource ComboBoxForegroundDisabled}" />
    </Style>
    <Style Selector="^ /template/ TextBlock#PlaceholderTextBlock">
      <Setter Property="Foreground" Value="{DynamicResource ComboBoxForegroundDisabled}" />
    </Style>
    <Style Selector="^ /template/ PathIcon#DropDownGlyph">
      <Setter Property="Foreground" Value="{DynamicResource ComboBoxDropDownGlyphForegroundDisabled}" />
    </Style>
  </Style>
</ControlTheme>

查看源码:

  • 1、与WPF不同:
  <Grid ColumnDefinitions="*,32"/>
  <!--等同于WPF-->
 <Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="32" />
    </Grid.ColumnDefinitions>
  </Grid>
  • 2、可以看到ComboBox是不能输入的,因此我们需要更改Template模板。

二、更改Template,并添加水印

 <Style Selector="ComboBox">
     <Setter Property="BorderBrush" Value="{DynamicResource ThemeBorderMidBrush}" />
     <Setter Property="BorderThickness" Value="{DynamicResource ThemeBorderThickness}" />
     <Setter Property="HorizontalContentAlignment" Value="Stretch" />
     <Setter Property="VerticalContentAlignment" Value="Center" />
     <Setter Property="Padding" Value="4" />
     <Setter Property="MinHeight" Value="20" />
     <Setter Property="PlaceholderForeground" Value="{DynamicResource ThemeForegroundBrush}" />
     <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Disabled" />
     <Setter Property="Template">
         <ControlTemplate>
             <DataValidationErrors>
                 <Grid ColumnDefinitions="*,32">
                     <Border
                         x:Name="Background"
                         Grid.Column="0"
                         Grid.ColumnSpan="2"
                         MinWidth="{DynamicResource ComboBoxThemeMinWidth}"
                         Background="{TemplateBinding Background}"
                         BorderBrush="{TemplateBinding BorderBrush}"
                         BorderThickness="{TemplateBinding BorderThickness}"
                         CornerRadius="{TemplateBinding CornerRadius}" />
                     <Border
                         x:Name="HighlightBackground"
                         Grid.Column="0"
                         Grid.ColumnSpan="2"
                         Background="{DynamicResource ComboBoxBackgroundUnfocused}"
                         BorderBrush="{DynamicResource ComboBoxBackgroundBorderBrushUnfocused}"
                         BorderThickness="{TemplateBinding BorderThickness}"
                         CornerRadius="{TemplateBinding CornerRadius}"
                         IsVisible="False" />
                     <TextBlock
                         Name="hint"
                         Margin="8,0,0,0"
                         VerticalAlignment="Center"
                         FontSize="12"
                         Foreground="White"
                         IsVisible="{TemplateBinding SelectionBoxItem,
                                                     Converter={x:Static ObjectConverters.IsNull}}"
                         Opacity="0.8"
                         Text="请输入用户名!!!" />
                     <TextBox
                         x:Name="PlaceholderTextBlock"
                         Grid.Column="0"
                         Margin="{TemplateBinding Padding}"
                         HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                         VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                         Background="Transparent"
                         BorderThickness="0"
                         Foreground="{TemplateBinding Foreground}"
                         IsInactiveSelectionHighlightEnabled="False"
                         Text="{TemplateBinding SelectionBoxItem}" />
                     <Border
                         x:Name="DropDownOverlay"
                         Grid.Column="1"
                         Width="30"
                         Margin="0,1,1,1"
                         HorizontalAlignment="Right"
                         Background="Transparent"
                         IsVisible="False" />

                     <PathIcon
                         x:Name="DropDownGlyph"
                         Grid.Column="1"
                         Width="12"
                         Height="12"
                         Margin="0,0,10,0"
                         HorizontalAlignment="Right"
                         VerticalAlignment="Center"
                         Data="M1939 486L2029 576L1024 1581L19 576L109 486L1024 1401L1939 486Z"
                         Foreground="{DynamicResource ComboBoxDropDownGlyphForeground}"
                         IsHitTestVisible="False"
                         UseLayoutRounding="False" />
                     <Popup
                         Name="PART_Popup"
                         MinWidth="{Binding Bounds.Width, RelativeSource={RelativeSource TemplatedParent}}"
                         MaxHeight="{TemplateBinding MaxDropDownHeight}"
                         InheritsTransform="True"
                         IsLightDismissEnabled="True"
                         IsOpen="{TemplateBinding IsDropDownOpen,
                                                  Mode=TwoWay}"
                         PlacementTarget="Background"
                         WindowManagerAddShadowHint="False">
                         <Border
                             x:Name="PopupBorder"
                             Padding="{DynamicResource ComboBoxDropdownBorderPadding}"
                             HorizontalAlignment="Stretch"
                             Background="{DynamicResource ComboBoxDropDownBackground}"
                             BorderBrush="{DynamicResource ComboBoxDropDownBorderBrush}"
                             BorderThickness="{DynamicResource ComboBoxDropdownBorderThickness}"
                             CornerRadius="{DynamicResource OverlayCornerRadius}">
                             <ScrollViewer
                                 HorizontalScrollBarVisibility="{TemplateBinding ScrollViewer.HorizontalScrollBarVisibility}"
                                 IsDeferredScrollingEnabled="{TemplateBinding (ScrollViewer.IsDeferredScrollingEnabled)}"
                                 VerticalScrollBarVisibility="{TemplateBinding ScrollViewer.VerticalScrollBarVisibility}">
                                 <ItemsPresenter
                                     Name="PART_ItemsPresenter"
                                     Margin="{DynamicResource ComboBoxDropdownContentMargin}"
                                     ItemsPanel="{TemplateBinding ItemsPanel}" />
                             </ScrollViewer>
                         </Border>
                     </Popup>
                 </Grid>
             </DataValidationErrors>
         </ControlTemplate>
     </Setter>
 </Style>

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2259406.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

TMS320C55x DSP芯片结构和CPU外围电路

第2章 DSP芯片结构和CPU外围电路 文章目录 第2章 DSP芯片结构和CPU外围电路TMS320C55x处理器的特点TMS320c55x CPU单元指令缓冲(Instruction Buffer Unit) I单元程序流程(Program Flow Unit) P单元地址数据(Address-data Flow Unit) A单元数据计算(Data Computation Unit) D单元…

Oracle 与 达梦 数据库 对比

当尝试安装了达梦数据库后&#xff0c;发现达梦真的和Oracle数据库太像了&#xff0c;甚至很多语法都相同。 比如&#xff1a;Oracle登录数据库采用sqlplus&#xff0c;达梦采用disql。 比如查看数据视图&#xff1a;达梦和Oracle都有 v$instance、v$database、dba_users等&a…

数据结构之五:排序

void*类型的实现&#xff1a;排序&#xff08;void*类型&#xff09;-CSDN博客 一、插入排序 1、直接插入排序 思想&#xff1a;把待排序的数据逐个插入到一个已经排好序的有序序列中&#xff0c;直到所有的记录插入完为止&#xff0c;得到一个新的有序序列 。 单趟&#x…

JavaWeb:JavaScript

学习 资源1 学习资源 2 黑马javaweb 1、引入方式 内部脚本&#xff1a; <script>内容</script> 外部脚本&#xff1a; <script src"js/test.js"></script> 2、基础语法 注释&#xff1a;// /* */ 结尾分号可有可无 大括号表示代码块 …

MySQL其五,索引详解,逻辑架构,SQL优化等概念

目录 一、索引 1、索引的概念 2、索引的优缺点 3、添加索引的原则 4、索引的分类 5、索引如何使用 6、存储过程讲解 7、测试索引的效率 7、索引的数据结构 8、覆盖索引&#xff08;SQL优化的点&#xff09; 9、最佳左前缀法则&#xff08;SQL优化的点&#xff09; 二…

考研数学【线性代数基础box(数二)】

本文是对数学二线性代数基础进行总结&#xff0c;一些及极其简单的被省略了&#xff0c;代数的概念稀碎&#xff0c;不如高数关联性高&#xff0c;所以本文仅供参考&#xff0c;做题请从中筛选&#xff01; 本文为初稿&#xff0c;后面会根据刷题和自己的理解继续更新 第一章…

全面解析租赁小程序的功能与优势

内容概要 租赁小程序正在逐渐改变人与物之间的互动方式。通过这些小程序&#xff0c;用户不仅可以轻松找到所需的租赁商品&#xff0c;还能够享受无缝的操作体验。为了给大家一个清晰的了解&#xff0c;下面我们将重点介绍几个核心功能。 建议&#xff1a;在选择租赁小程序时&…

Linux DNS 协议概述

1. DNS 概述 互联网中&#xff0c;一台计算机与其他计算机通信时&#xff0c;通过 IP 地址唯一的标志自己。此时的 IP 地址就类似于我们日常生活中的电话号码。但是&#xff0c;这种纯数字的标识是比较难记忆的&#xff0c;而且数量也比较庞大。例如&#xff0c;每个 IPv4 地址…

Java使用ORM Bee自动生成Javabean.

Java使用ORM Bee自动生成Javabean. 配置数据库连接,添加了pom.xml依赖后,就可以写Java代码,自动生成Javabean了. 可参考:https://gitee.com/automvc/bee https://github.com/automvc/bee 还可以生成字段文件, 这样可以避免硬编码引用字段,速度也比反射快. package org.tea…

【MySQL中多表查询和函数】

目录 1.多表查询 1.1 外键 1.2 链接查询 2.MySQL函数 内置函数简介 数值函数 字符串函数 时间日期函数 条件判断操作 开窗函数 1.多表查询 本质&#xff1a;把多个表通过主外键关联关系链接&#xff08;join&#xff09;合并成一个大表&#xff0c;在去单表查询操作…

二维码数据集,使用yolov,voc,coco标注,3044张各种二维码原始图片(未图像增强)

二维码数据集&#xff0c;使用yolov&#xff0c;voc&#xff0c;coco标注&#xff0c;3044张各种二维码原始图片&#xff08;未图像增强&#xff09; 数据集分割 训练组70&#xff05; 2132图片 有效集20&#xff05; 607图片 测试集10&#xff05; 305图…

MySQL多表查询时有哪些连接方式?

大家好&#xff0c;我是锋哥。今天分享关于【MySQL多表查询时有哪些连接方式?】面试题。希望对大家有帮助&#xff1b; MySQL多表查询时有哪些连接方式? 1000道 互联网大厂Java工程师 精选面试题-Java资源分享网 在 MySQL 中进行多表查询时&#xff0c;常见的连接方式有以下…

LBS 开发微课堂|通过openGL ES轻松实现建筑物渲染及动画

为了让广大开发者 更深入地了解 百度地图开放平台的 技术能力 轻松掌握满满的 技术干货 更加简单地接入 位置服务 我们特别推出了 “位置服务&#xff08;LBS&#xff09;开发微课堂” 系列技术案例 第五期的主题是 通过openGL ES轻松实现 建筑物渲染及动画 对于…

Java——IO流(下)

一 (字符流扩展) 1 字符输出流 (更方便的输出字符——>取代了缓冲字符输出流——>因为他自己的节点流) (PrintWriter——>节点流——>具有自动行刷新缓冲字符输出流——>可以按行写出字符串&#xff0c;并且可通过println();方法实现自动换行) 在Java的IO流中…

SQLServer到MySQL的数据高效迁移方案分享

SQL Server数据集成到MySQL的技术案例分享 在企业级数据管理中&#xff0c;跨平台的数据集成是一个常见且关键的任务。本次我们将探讨如何通过轻易云数据集成平台&#xff0c;将巨益OMS系统中的退款单明细表从SQL Server高效、安全地迁移到MySQL数据库中。具体方案名称为“7--…

每日计划-1213

1. 完成 SQL2 查询多列 https://www.nowcoder.com/exam/oj?page1tabSQL%E7%AF%87topicId199 2. 八股部分 1) C 中面向对象编程如何实现数据隐藏&#xff1f; 在c中&#xff0c;可以将数据成员声明为私有或受保护&#xff0c;并提供公有的成员函数来访问和修改这些数据成员&am…

ORACLE 导入导出数据库(包含表结构和数据)

导出 1、进入本地oracle 驱动安装目录下–> 进入CMD 2、输入命令 exp 用户名/密码10.xx.xx.xx:1521/orcl fileexport.dmp 3、查看导出的文件 导入 1、进入本地oracle 驱动安装目录下–> 进入CMD 2、输入命令 imp 用户名/密码10.xx.xx.xx:1521/orcl fully ignorey…

Qt之将源代码封装成库文件使用(五)

Qt开发 系列文章 - Code-To-Library&#xff08;五&#xff09; 目录 前言 一、库文件 二、直接封装方式 1.静态库封装 2.动态库封装 3.其它库类型 三、二次重写封装 四、库的使用 1.移植库及头文件 2.添加外部库 总结 前言 库文件是计算机上的一类文件&#xff0c…

视频监控汇聚平台方案设计:Liveweb视频智能监管系统方案技术特点与应用

随着科技的发展&#xff0c;视频监控平台在各个领域的应用越来越广泛。然而&#xff0c;当前的视频监控平台仍存在一些问题&#xff0c;如视频质量不高、监控范围有限、智能化程度不够等。这些问题不仅影响了监控效果&#xff0c;也制约了视频监控平台的发展。 为了解决这些问…

跨平台开发技术的探索:从 JavaScript 到 Flutter

随着多平台支持和用户体验一致性在应用程序开发中变得越来越重要,开发者面临的挑战是如何在不同平台上保持代码的可维护性和高效性。本文将探讨如何利用现代技术栈,包括 Flutter、JavaScript、HTML5、WebAssembly、TypeScript 和 Svelte,在统一的平台上进行高效的跨平台开发…