简体   繁体   中英

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>

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM