Liam
Liam

Reputation: 493

ComboBox VirtualizingStackPanel WPF Programmatically

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>

Upvotes: 1

Views: 302

Answers (1)

mami
mami

Reputation: 606

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)));

Upvotes: 2

Related Questions