gpt4 book ai didi

c# - 无焦点 ComboBoxItem

转载 作者:太空狗 更新时间:2023-10-29 22:10:20 24 4
gpt4 key购买 nike

我正在处理通常包含大量数据的 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 元素都不可聚焦,但不会使程序崩溃。


GIF of Problem


示例代码

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com