Home > Software engineering >  ComboBox VirtualizingStackPanel WPF Programmatically
ComboBox VirtualizingStackPanel WPF Programmatically

Time:11-02

I'm programmatically creating a combobox but I don't know how to add the virtualizing stack panel with C#.

C#

  ComboBox newCombo = new ComboBox();
  newCombo.IsEditable = true;
  newCombo.DisplayMemberPath = "DisplayName";
  newCombo.SelectedValuePath = "Value";
  newCombo.SelectedValue = "Value";

XAML

 <ComboBox Grid.Row="0" Grid.Column="1" x:Name="cbOrigin" Grid.ColumnSpan="2" IsEditable="True"
                DisplayMemberPath="DisplayName"
                SelectedValuePath="Value"
                SelectedValue="{Binding Path=Value}" >
                <ComboBox.ItemsPanel>
                    <ItemsPanelTemplate>
                        <VirtualizingStackPanel />
                    </ItemsPanelTemplate>
                </ComboBox.ItemsPanel>
            </ComboBox>

CodePudding user response:

Maybe like this:

ComboBox newCombo = new ComboBox();
newCombo.IsEditable = true;
newCombo.DisplayMemberPath = "DisplayName";
newCombo.SelectedValuePath = "Value";
newCombo.SelectedValue = "Value";
newCombo.ItemsPanel = new ItemsPanelTemplate(new 
FrameworkElementFactory(typeof(VirtualizingStackPanel)));
  • Related