相当于 WinRT 中的可编辑组合框?

2024-03-21

标准(桌面)Windows 组合框具有三种样式;简单、下拉和下拉列表。下拉菜单的工作方式类似于编辑控件和组合框,而下拉列表不允许编辑。

我是否遗漏了某些内容,或者 Windows 8 商店应用程序中的 XAML ComboBox 控件仅支持下拉列表样式?

当我遇到这个问题时,我正在实施一些东西,而且我看得越多,它似乎就越不受支持。

我真的需要用编辑控件和列表框替换屏幕中的组合框吗?

Yuck.


感谢 edward.ho.tpe 的回答,我用一个方法给自己写了一个小 EditableComboBoxTextBox里面一个ComboBoxItem。 如果您想多次使用它,最好创建一个 UserControl。

然而我就是这样做的:

样式:

<SolidColorBrush x:Key="TransparentBrush" Color="Transparent"/>
<Style x:Key="ComboBoxItemTextBox" TargetType="TextBox">
    <Setter Property="MinWidth" Value="{ThemeResource TextControlThemeMinWidth}"/>
    <Setter Property="MinHeight" Value="{ThemeResource TextControlThemeMinHeight}"/>
    <Setter Property="Foreground" Value="{ThemeResource TextBoxForegroundThemeBrush}"/>
    <Setter Property="Background" Value="{StaticResource TransparentBrush}"/>
    <Setter Property="BorderBrush" Value="{StaticResource TransparentBrush}"/>
    <Setter Property="SelectionHighlightColor" Value="{ThemeResource TextSelectionHighlightColorThemeBrush}"/>
    <Setter Property="BorderThickness" Value="0"/>
    <Setter Property="FontFamily" Value="{ThemeResource ContentControlThemeFontFamily}"/>
    <Setter Property="FontSize" Value="{ThemeResource ControlContentThemeFontSize}"/>
    <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Hidden"/>
    <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Hidden"/>
    <Setter Property="ScrollViewer.IsDeferredScrollingEnabled" Value="False"/>
    <Setter Property="Padding" Value="{ThemeResource TextControlThemePadding}"/>
    <Setter Property="Margin" Value="-10,0,0,0"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="TextBox">
                <Grid>
                    <Grid.Resources>
                        <Style x:Name="DeleteButtonStyle" TargetType="Button">
                            <Setter Property="Template">
                                <Setter.Value>
                                    <ControlTemplate TargetType="Button">
                                        <Grid>
                                            <VisualStateManager.VisualStateGroups>
                                                <VisualStateGroup x:Name="CommonStates">
                                                    <VisualState x:Name="Normal"/>
                                                    <VisualState x:Name="PointerOver">
                                                        <Storyboard>
                                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="BackgroundElement">
                                                                <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource TextBoxButtonPointerOverBackgroundThemeBrush}"/>
                                                            </ObjectAnimationUsingKeyFrames>
                                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="BorderElement">
                                                                <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource TextBoxButtonPointerOverBorderThemeBrush}"/>
                                                            </ObjectAnimationUsingKeyFrames>
                                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="GlyphElement">
                                                                <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource TextBoxButtonPointerOverForegroundThemeBrush}"/>
                                                            </ObjectAnimationUsingKeyFrames>
                                                        </Storyboard>
                                                    </VisualState>
                                                    <VisualState x:Name="Pressed">
                                                        <Storyboard>
                                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="BackgroundElement">
                                                                <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource TextBoxButtonPressedBackgroundThemeBrush}"/>
                                                            </ObjectAnimationUsingKeyFrames>
                                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="BorderElement">
                                                                <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource TextBoxButtonPressedBorderThemeBrush}"/>
                                                            </ObjectAnimationUsingKeyFrames>
                                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="GlyphElement">
                                                                <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource TextBoxButtonPressedForegroundThemeBrush}"/>
                                                            </ObjectAnimationUsingKeyFrames>
                                                        </Storyboard>
                                                    </VisualState>
                                                    <VisualState x:Name="Disabled">
                                                        <Storyboard>
                                                            <DoubleAnimation Duration="0" To="0" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="BackgroundElement"/>
                                                            <DoubleAnimation Duration="0" To="0" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="BorderElement"/>
                                                        </Storyboard>
                                                    </VisualState>
                                                </VisualStateGroup>
                                            </VisualStateManager.VisualStateGroups>
                                            <Border x:Name="BorderElement" BorderBrush="{ThemeResource TextBoxButtonBorderThemeBrush}" BorderThickness="{TemplateBinding BorderThickness}"/>
                                            <Border x:Name="BackgroundElement" Background="{ThemeResource TextBoxButtonBackgroundThemeBrush}" Margin="{TemplateBinding BorderThickness}">
                                                <TextBlock x:Name="GlyphElement" AutomationProperties.AccessibilityView="Raw" Foreground="{ThemeResource TextBoxButtonForegroundThemeBrush}" FontStyle="Normal" FontFamily="{ThemeResource SymbolThemeFontFamily}" HorizontalAlignment="Center" Text="&#xE0A4;" VerticalAlignment="Center"/>
                                            </Border>
                                        </Grid>
                                    </ControlTemplate>
                                </Setter.Value>
                            </Setter>
                        </Style>
                    </Grid.Resources>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="Auto"/>
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="*"/>
                    </Grid.RowDefinitions>
                    <VisualStateManager.VisualStateGroups>
                        <VisualStateGroup x:Name="CommonStates">
                            <VisualState x:Name="Disabled">
                                <Storyboard>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="BackgroundElement">
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource TextBoxDisabledBackgroundThemeBrush}"/>
                                    </ObjectAnimationUsingKeyFrames>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="BorderElement">
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource TextBoxDisabledBorderThemeBrush}"/>
                                    </ObjectAnimationUsingKeyFrames>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentElement">
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource TextBoxDisabledForegroundThemeBrush}"/>
                                    </ObjectAnimationUsingKeyFrames>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="PlaceholderTextContentPresenter">
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource TextBoxDisabledForegroundThemeBrush}"/>
                                    </ObjectAnimationUsingKeyFrames>
                                </Storyboard>
                            </VisualState>
                            <VisualState x:Name="Normal">
                                <Storyboard>
                                    <DoubleAnimation Duration="0" To="{ThemeResource TextControlBackgroundThemeOpacity}" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="BackgroundElement"/>
                                    <DoubleAnimation Duration="0" To="{ThemeResource TextControlBorderThemeOpacity}" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="BorderElement"/>
                                </Storyboard>
                            </VisualState>
                            <VisualState x:Name="PointerOver">
                                <Storyboard>
                                    <DoubleAnimation Duration="0" To="{ThemeResource TextControlPointerOverBackgroundThemeOpacity}" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="BackgroundElement"/>
                                    <DoubleAnimation Duration="0" To="{ThemeResource TextControlPointerOverBorderThemeOpacity}" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="BorderElement"/>
                                </Storyboard>
                            </VisualState>
                            <VisualState x:Name="Focused"/>
                        </VisualStateGroup>
                        <VisualStateGroup x:Name="ButtonStates">
                            <VisualState x:Name="ButtonVisible"/>
                            <VisualState x:Name="ButtonCollapsed"/>
                        </VisualStateGroup>
                    </VisualStateManager.VisualStateGroups>
                    <Border x:Name="BackgroundElement" Background="{TemplateBinding Background}" Grid.ColumnSpan="2" Margin="{TemplateBinding BorderThickness}" Grid.Row="1" Grid.RowSpan="1"/>
                    <Border x:Name="BorderElement" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Grid.ColumnSpan="2" Grid.Row="1" Grid.RowSpan="1"/>
                    <ContentPresenter x:Name="HeaderContentPresenter" Grid.ColumnSpan="2" ContentTemplate="{TemplateBinding HeaderTemplate}" Content="{TemplateBinding Header}" Foreground="{ThemeResource TextBoxForegroundHeaderThemeBrush}" FontWeight="Semilight" Margin="0,4,0,4" Grid.Row="0"/>
                    <ScrollViewer x:Name="ContentElement" AutomationProperties.AccessibilityView="Raw" HorizontalScrollMode="{TemplateBinding ScrollViewer.HorizontalScrollMode}" HorizontalScrollBarVisibility="{TemplateBinding ScrollViewer.HorizontalScrollBarVisibility}" IsTabStop="False" IsHorizontalRailEnabled="{TemplateBinding ScrollViewer.IsHorizontalRailEnabled}" IsVerticalRailEnabled="{TemplateBinding ScrollViewer.IsVerticalRailEnabled}" IsDeferredScrollingEnabled="{TemplateBinding ScrollViewer.IsDeferredScrollingEnabled}" Margin="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}" Grid.Row="1" VerticalScrollBarVisibility="{TemplateBinding ScrollViewer.VerticalScrollBarVisibility}" VerticalScrollMode="{TemplateBinding ScrollViewer.VerticalScrollMode}" ZoomMode="Disabled"/>
                    <ContentControl x:Name="PlaceholderTextContentPresenter" Grid.ColumnSpan="2" Content="{TemplateBinding PlaceholderText}" Foreground="{ThemeResource TextBoxPlaceholderTextThemeBrush}" IsHitTestVisible="False" IsTabStop="False" Margin="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}" Grid.Row="1"/>
                    <Button x:Name="DeleteButton" BorderThickness="{TemplateBinding BorderThickness}" Grid.Column="1" FontSize="{TemplateBinding FontSize}" IsTabStop="False" Grid.Row="1" Style="{StaticResource DeleteButtonStyle}" Visibility="Collapsed" VerticalAlignment="Stretch"/>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

组合框:

<ComboBox SelectionChanged="ComboBox_SelectionChanged">
    <ComboBoxItem IsSelected="True">
        <TextBox x:Name="tbComboBox" Style="{StaticResource ComboBoxItemTextBox}" KeyDown="tbComboBox_KeyDown"/>
    </ComboBoxItem>
    <ComboBoxItem>Item 1</ComboBoxItem>
    <ComboBoxItem>Item 2</ComboBoxItem>
    <ComboBoxItem>Item 3</ComboBoxItem>
    <ComboBoxItem>Item 4</ComboBoxItem>
</ComboBox>

事件处理程序:

private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    if (e.AddedItems.Count == 1 && e.AddedItems[0] != (sender as ComboBox).Items[0])
    {
        (sender as ComboBox).SelectedIndex = 0;
        tbComboBox.Text = (e.AddedItems[0] as ComboBoxItem).Content as String;
    }
}

private void tbComboBox_KeyDown(object sender, KeyRoutedEventArgs e)
{
    if (e.Key == Windows.System.VirtualKey.Space)
    {
        if (tbComboBox.SelectionLength > 0)
        {
            tbComboBox.Text = tbComboBox.Text.Remove(tbComboBox.SelectionStart, tbComboBox.SelectionLength);
            tbComboBox.SelectionLength = 0;
        }
        int pos = tbComboBox.SelectionStart;
        tbComboBox.Text = tbComboBox.Text.Insert(pos, " ");
        tbComboBox.SelectionStart = pos + 1;
        e.Handled = true;
    }
}

基本上这是什么:

正如 edward.ho.tpe 建议的那样TextBox作为 a 的第一项的内容ComboBox. The TextBox有一个自定义样式,可以删除背景、边框,将其向左移动一点(可选 - 但我喜欢它,因为它提供了更多的书写空间),并将删除按钮更改为保持折叠状态。 这SelectionChanged事件处理程序确保TextBox保持SelectedItem并且只改变Text根据所选项目的内容和KeyDown事件处理程序处理空格键,否则会触发下拉菜单ComboBox.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

相当于 WinRT 中的可编辑组合框? 的相关文章

随机推荐

  • DB2 v9.5 类型 2 驱动程序的 JPA 2.0 Provider Hibernate 3.6 在配置准备中抛出异常

    JPA 2 0 Provider Hibernate 在为实体管理器工厂准备配置时抛出异常 我正在使用 DB2 v9 5 数据库和 DB2 v9 5 JDBC 类型 2 驱动程序 java sql SQLException IBM JDBC
  • Python 有同步吗?

    The 同步手册页 http linux die net man 2 sync says sync 导致对文件元数据和数据的所有缓冲修改 写入底层文件系统 Python 是否有调用来执行此操作 附 不是fsync http docs pyt
  • 在运行时更改 AR core 中对象的纹理

    我正在创建一个应用程序 我需要在其中更改 3d 对象的纹理 图案 我正在使用 AR Core SDK 和 android 我已经使用了下面的代码 但它不起作用 Texture builder setSource this R drawabl
  • Clojure 中的块注释

    如何在 Clojure 中注释多行 其实 还有办法 comment 定义嘿 嘿 对我进行检查
  • 使用 DT(DataTables 按钮扩展)导出表格时保持格式

    我制作了一个闪亮的应用程序 有人上传文件 计算一些比率 并且可以使用阈值滑块来格式化这些比率 我用DT formatStyle为此 它工作得非常好 据我了解这个函数 它创建一个回调来处理条件格式 然后 我想使用按钮扩展导出数据DT 我想在导
  • 正则表达式在 if 语句中将字符串与空格匹配(使用引号?)

    我将如何进行如下所示的正则表达式匹配 但在 This 周围加上引号 就像在现实世界中 This 将是一个可以在其中包含空格的字符串 bin bash text This is just a test string if text This
  • 分布式事务:.NET Framework 与 .NET Core

    我有以下代码示例 static void Main string args TransactionManager DistributedTransactionStarted sender eventArgs gt Console Write
  • SSE 和 AVX MoveMask 的用途是什么

    问题 MoveMask 的目的或意图是什么 学习如何使用 x86 x86 64 汇编 SSE AVX 的最佳地点是哪里 我可以更有效地编写代码吗 提问原因 我有一个用 F for NET 编写的使用 SSE2 的函数 我使用 AVX2 写了
  • 仅检索 Java 类中声明的静态字段

    我有以下课程 public class Test public static int a 0 public int b 1 是否可以使用反射来仅获取静态字段的列表 我知道我可以获得所有字段的数组Test class getDeclaredF
  • Android Google 地图 v2 删除默认标记

    我在我的 Android 应用程序中实现了 Android Google Maps v2 没有任何问题 但是 地图包含一些我未包含的 默认标记 我的意思是 一些私人营业地点 是否可以从地图上删除这些标记 以便我只获得城市名称和街道名称 只需
  • 捕获 Camera2 预览帧返回空缓冲区

    我一直在开发一个简单的 Android 应用程序 旨在将流式相机帧从 Android Camera2 API 管道传递到我的算法 我已经制作了几个应用程序 忠实地使用 Android Camera1 API 执行此操作 但即使在检查 Goo
  • 设置证书验证位置时出错

    OS Ubuntu 16 01 PHP 7 0 根据一些谷歌搜索以及 StackOverflow 上问题的输入 我设置以下 Curl 选项 CURLOPT SSL VERIFYPEER gt true CURLOPT SSL VERIFYH
  • 在emacs中使用flymake和tramp远程运行pyflakes?

    我正在尝试使用 Flymake 来运行 pyflakes 如建议的那样here http www emacswiki org cgi bin wiki PythonMode toc9 这对于本地文件来说效果很好 并且几乎可以通过一些调整来处
  • Bootstrap网格系统-如何使两列高度相等? [复制]

    这个问题在这里已经有答案了 如何在 Bootstrap 网格列中使两列具有相同的高度 item text padding 30px important Flex center display flex align items center
  • 获取文件的扩展名而不在路径中提供扩展名

    当您指定除扩展名之外的整个路径时 是否可以获得文件的扩展名 例如 C Users Administrator Pictures BlueHillsTest Thanks Directory GetFiles http msdn micros
  • Selenium 2 和 JUnit4:如何捕获异常屏幕截图?

    我只想在意外异常时捕获屏幕截图 Note This answer could be outdated The answer is based on Selenium 2 15 Using 测试观察者 http kentbeck github
  • 正则表达式匹配所有字母数字和某些特殊字符?

    我正在尝试让正则表达式工作 该正则表达式将允许所有字母数字字符 大写字母和非大写字母以及数字 但也允许空格 正斜杠 破折号 并加上 我已经开始了 但到目前为止还没有成功 如果你想允许only那些 你还需要使用锚点 and a zA Z0 9
  • 了解libuv/epoll/非阻塞网络IO

    我试图了解非阻塞网络 IO 是如何工作的Node js libuv 我已经发现了fileIO 是使用完成的libuv工作线程 因此 在后台线程中 不过很多地方都说networkIO 是使用系统调用以非阻塞方式完成的 例如epoll kque
  • 使用 Delphi 通过 LDAP 进行 Active Directory 身份验证,并使用 [email protected]

    正如您从下面的代码片段中看到的 我目前正在使用 adshlp 和 ActiveDs TLB 从当前登录的用户收集有关 AD 的信息 我有一个表格 允许用户输入他们的 AD 密码 并在允许访问系统之前验证其是否正确 这很好 我现在遇到的问题是
  • 相当于 WinRT 中的可编辑组合框?

    标准 桌面 Windows 组合框具有三种样式 简单 下拉和下拉列表 下拉菜单的工作方式类似于编辑控件和组合框 而下拉列表不允许编辑 我是否遗漏了某些内容 或者 Windows 8 商店应用程序中的 XAML ComboBox 控件仅支持下