I am working with ComboBox elements that often contain very large quantities of data; ~250000 data entries.
This works fine when a ComboBox is set up a little like this.
<ComboBox ItemsSource="{Binding Items}">
<ComboBox.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel />
</ItemsPanelTemplate>
</ComboBox.ItemsPanel>
</ComboBox>
However, some custom modifications of the ComboBox I am working with require the ComboBoxItem elements to not be focusable. I achieved this by using a setter in the ComboBox.ItemContainerStyle.
<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>
But there is a problem with this. It works fine until an object has been selected. Then when a user tries to open the ComboBox again, it crashes the program.
My question is, how can the ComboBox be set up so all its ComboBoxItem elements are not focusable, but it does not crash the program.
Example Code
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();
}
}
}
