简体   繁体   English

在运行时处理 ListBox 内的 ComboBox(XAML、WPF、C#)

[英]Handling a ComboBox inside a ListBox at runtime (XAML, WPF, C#)

I have a Listbox with several items.我有一个包含多个项目的列表框。 They are being added dynamically at runtime.它们是在运行时动态添加的。

Each of these items has a Combobox with the same number of items, that should also be added at runtime.这些项目中的每一个都有一个 Combobox 具有相同数量的项目,也应该在运行时添加。

<ListBox x:Name="ListBox_Parcels" 
                 ItemsSource="{Binding}"
                 SelectionMode="Multiple"
                 SelectionChanged="ListBox_Parcels_SelectionChanged">
           <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid Background="Transparent" Cursor="Hand">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto" />
                            <ColumnDefinition Width="Auto" />
                            <ColumnDefinition Width="Auto" />
                        </Grid.ColumnDefinitions>
                        <TextBlock x:Name="tb_title"
                                    Grid.Column="0" 
                                    Text="{Binding Name}" />
                        <ComboBox x:Name="Combobox_ParcelPreset"
                                  Grid.Column="1"
                                  SelectionChanged="ComboBox_ParcelPreset_SelectionChanged">
                            <ComboBoxItem Content="initItem1"/>
                            <ComboBoxItem Content="initItem2"/>
                        </ComboBox>
                    </Grid>
            </DataTemplate>
      </ListBox.ItemTemplate>
</ListBox>

Now I want to add an Item (yia background code at runtime) to all the ComboBox_ParcelPreset that are inside the listbox.现在我想向列表框中的所有ComboBox_ParcelPreset添加一个项目(运行时的背景代码)。 I want to be able to set the selected index of a specific ComboBox inside the listbox and when the selection of the Combobox changed by the user to get the index both of the combobox and the listbox.我希望能够在列表框中设置特定 ComboBox 的选定索引,并且当用户更改 Combobox 的选择以获取 combobox 的索引时。

My problem is, that ComboBox_ParcelPreset does not exist, and ListBox_Parcels does not have any way to access the ComboBox.我的问题是, ComboBox_ParcelPreset不存在, ListBox_Parcels没有任何方法可以访问 ComboBox。

        Itemlist_Parcels = new ObservableCollection<ClParcelItem>();
        ListBox_Parcels.DataContext = Itemlist_Parcels;

And if i change the selection in the combobox, i also do not get the index of the listbox.如果我更改 combobox 中的选择,我也不会得到列表框的索引。

Any hints welcome.欢迎任何提示。

What helped me was: How to access a specific item in a Listbox with DataTemplate?帮助我的是: 如何使用 DataTemplate 访问列表框中的特定项目?

for (int i = 0; i< ListBox_Parcels.Items.Count; i++)
            {
                ListBoxItem lbi = (ListBoxItem)ListBox_Parcels.ItemContainerGenerator.ContainerFromIndex(i);
                ComboBox cb = FindDescendant<ComboBox>(lbi);   
                cb.Items.Add(preset[0]);
                editing_parcel = (ClParcelItem)ListBox_Parcels.Items[i];
                if (ParcelNames.Exists(n => n == editing_parcel.Name))
                {
                    cb.SelectedIndex = cb.Items.Count - 1;
                }

            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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