- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在处理通常包含大量数据的 ComboBox
元素; ~250000 个数据条目。
当 ComboBox
像这样设置时,这会很好地工作。
<ComboBox ItemsSource="{Binding Items}">
<ComboBox.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel />
</ItemsPanelTemplate>
</ComboBox.ItemsPanel>
</ComboBox>
但是,我正在使用的 ComboBox
的一些自定义修改要求 ComboBoxItem
元素不可聚焦。我通过在 ComboBox.ItemContainerStyle
中使用 setter 实现了这一点。
<ComboBox ItemsSource="{Binding Items}">
<ComboBox.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel />
</ItemsPanelTemplate>
</ComboBox.ItemsPanel>
<ComboBox.ItemContainerStyle>
<Style TargetType="ComboBoxItem">
<Setter
Property="Focusable"
Value="False" />
</Style>
</ComboBox.ItemContainerStyle>
</ComboBox>
但是这有一个问题。它工作正常,直到一个对象被选中。然后,当用户再次尝试打开 ComboBox
时,程序就会崩溃。
我的问题是,如何设置 ComboBox
使其所有 ComboBoxItem
元素都不可聚焦,但不会使程序崩溃。
示例代码
XAML
<Window x:Class="FocusableTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title="MainWindow"
Height="450"
Width="800">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="2*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="2*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Viewbox Stretch="Uniform"
Grid.ColumnSpan="3">
<Label Content="Welcome"
FontWeight="Bold"
HorizontalAlignment="Center"
VerticalAlignment="Center"/>
</Viewbox>
<StackPanel Grid.Row="1"
Grid.Column="1">
<ComboBox ItemsSource="{Binding Items}">
<ComboBox.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel />
</ItemsPanelTemplate>
</ComboBox.ItemsPanel>
<ComboBox.ItemContainerStyle>
<Style TargetType="ComboBoxItem">
<Setter
Property="Focusable"
Value="False" />
</Style>
</ComboBox.ItemContainerStyle>
</ComboBox>
</StackPanel>
</Grid>
</Window>
C#
using System.Collections.ObjectModel;
using System.Security.Cryptography;
using System.Text;
namespace FocusableTest
{
public partial class MainWindow
{
public MainWindow()
{
for (int i = 0; i < 250000; i++)
{
Items.Add(GetUniqueKey());
}
InitializeComponent();
DataContext = this;
}
public ObservableCollection<string> Items { get; } = new ObservableCollection<string>();
private static string GetUniqueKey(int maxSize = 20)
{
char[] chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890".ToCharArray();
byte[] data = new byte[1];
using (RNGCryptoServiceProvider crypto = new RNGCryptoServiceProvider())
{
crypto.GetNonZeroBytes(data);
data = new byte[maxSize];
crypto.GetNonZeroBytes(data);
}
StringBuilder result = new StringBuilder(maxSize);
foreach (byte b in data)
{
result.Append(chars[b % (chars.Length)]);
}
return result.ToString();
}
}
}
最佳答案
我不确定你的设置究竟是什么样子,但我已经设法拥有一个 TextBox
和一个 ComboBox
,前者在从中选择项目时保持焦点后者。技巧是双重的 - 使 ComboBox
不可聚焦(因此在单击打开下拉菜单时它不会窃取焦点),并处理 ComboBoxItem
上的 PreviewGotKeyboardFocus
> 以防止它获得对悬停的关注,而不是将 Focusable
设置为 false
,这显然是导致问题的原因。这是代码摘录:
<StackPanel>
<TextBox></TextBox>
<ComboBox ItemsSource="{Binding Items}" Focusable="False">
<ComboBox.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel />
</ItemsPanelTemplate>
</ComboBox.ItemsPanel>
<ComboBox.ItemContainerStyle>
<Style TargetType="ComboBoxItem">
<EventSetter Event="PreviewGotKeyboardFocus"
Handler="ComboBoxItem_PreviewGotKeyboardFocus" />
</Style>
</ComboBox.ItemContainerStyle>
</ComboBox>
</StackPanel>
和代码隐藏:
private void ComboBoxItem_PreviewGotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
{
e.Handled = true;
}
关于c# - 无焦点 ComboBoxItem,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53352529/
我的项目中有以下ComboBox,但是如果我这样写的话,很困扰我:
我的目标是拥有一个 ComboBox,其中每个下拉项目都有一个特定的文本和一个与之关联的特定项目,例如,如果有人点击“blah” - 所选项目将是 3. 据我所知 - 只有一个“内容”同时代表文本和值
我希望我的 ComboBox 像这样在多个列中显示 ComboBoxItem: 谁能告诉我该怎么做? 我可以用这个:
加载控件时,我的组合框项目选择中的内容未显示在组合框中。它在我鼠标悬停后出现,然后开始工作。 这是默认选中的组合框项目
我正在处理通常包含大量数据的 ComboBox 元素; ~250000 个数据条目。 当 ComboBox 像这样设置时,这会很好地工作。
我有一个小应用程序,其中相同的 ComboBox 出现在多个位置,并且它始终具有相同的项目集。而不是每次我使用组合框时都这样做: 我在想最好让项目成为我可以
我想为 PASS 设置文本颜色如 GREEN和 FAIL 的文本颜色如 RED .我似乎无法找到解决方案。我需要在纯 XAML 中执行此操作。 最
我正在尝试以编程方式填充 ComboBox。我正在创建 ComboBoxItems 并想设置它们的文本(对最终用户可见的文本)和它们的值(用户选择它后我将在后台处理的对象。 然而,ComboBoxIt
我目前有一个如下所示的组合框: //XAML Awake & Alive 这会引发错误:以与符号“&”开头的实体引用或序列必须以分号“;”终止。 我假设我缺少某种转义序列以允许我使用 &。如何设
如何从 SmartGWT ComboBoxItem 获取所有记录? 我将记录添加为数据源。没有找到任何方法来检索所有记录。 提前谢谢您! 最佳答案 尝试 RecordList recordList=c
在下拉菜单中我得到这样的结果。找到附件图片。实际上,在“名称”字段中,“名称”和“描述”都显示为逗号(,)分隔。 final ComboBoxItem comboBoxItem = new Combo
我有一个看起来像这样的样式: var comboBoxItemStyle = new Style(typeof(ComboBoxItem)); comboBoxItemStyle.Setters.Ad
您好,我刚刚构建了一个小型转换计算器,我在其中添加了一个组合框,因此它不会那么困惑且易于管理。我不会向我的组合框添加一些选项,以便用户有不同的选项可供选择。但是,我将构建一个带有转换计算的小类,以便在
我有一个绑定(bind)到 ObservableCollection 的 ComboBox:
我有以下示例代码。奇怪的是,MouseMove 事件正确触发,但是当替换为 MouseEnter 时,当鼠标移到 ComboBoxItem 上时没有任何反应。知道如何解决这个问题吗?我实际上需要在用户
我有一个使用 DataTemplate 的 ListBox。单个 ListBoxItems 显示为 TextBlock 和 ComboBox。我现在想为 ListBoxItems 使用 Style,而
我正在使用标准的 wpf/mvvm 应用程序,我将组合框绑定(bind)到 ViewModel 上的集合。 我需要能够从下拉列表中取消选择一个项目。意思是,用户应该能够选择一些东西,然后决定他们想要取
如何获取 ComboBoxItem 的 ParentComboBox? 如果按下 Insert-Key,我想关闭一个打开的 ComboBox: var focusedElement = Keyboa
我需要找到 ComboBoxItem 所在的 ComboBox。 在代码隐藏中,我在单击 ComboBoxItem 时捕获了一个事件,但我不知道特定 ComboBoxItem 属于几个 ComboBo
我正在尝试在 smartgwt 的 ComboBoxItem 中添加带有 null 值的空白行,该值已绑定(bind)到来自数据库的数据源。 我也尝试过服务层、 Controller 层和数据库的改变
我是一名优秀的程序员,十分优秀!