wpf ItemContainerGenerator.ContainerFromItem()在VirtualizingStackPanel.IsVirtualizing=“False”时返回null

ruyhziif  于 2023-10-22  发布在  其他
关注(0)|答案(4)|浏览(117)

我遇到了类似的问题,但VirtualizingStackPanel.IsVirtualizing="False"没有解决我的问题。有没有人面临同样的问题?
问题是我有一个定制的组合框,

<Style TargetType="{x:Type MultiSelectionComboBox}"  >
    <Setter Property="VerticalContentAlignment" Value="Center"/>
    <Setter Property="ItemsPanel">
        <Setter.Value>
            <ItemsPanelTemplate>
                <StackPanel Orientation="Vertical"
                VerticalAlignment="Center"
                HorizontalAlignment="Center"
                VirtualizingStackPanel.IsVirtualizing="False"/>
            </ItemsPanelTemplate>
        </Setter.Value>
    </Setter>
    <Setter Property="ItemTemplate">
        <Setter.Value>
            <DataTemplate>
                <StackPanel Orientation="Horizontal" x:Name="ItemStack" VirtualizingStackPanel.IsVirtualizing="False">
                    <CheckBox x:Name="CheckBoxItem"
                        Command="{Binding SelectItem, RelativeSource={RelativeSource AncestorType={x:Type MultiSelectionComboBox}}}"
                        CommandParameter="{Binding Key}"
                              >
                    </CheckBox>
                    <TextBlock Text="{Binding DisplayText}"></TextBlock>
                </StackPanel>
            </DataTemplate>
        </Setter.Value>
    </Setter>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ComboBox}">
                <Grid x:Name="Placement" SnapsToDevicePixels="true">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="Auto"/>
                    </Grid.ColumnDefinitions>
                    <Border BorderThickness="1" BorderBrush="Black">
                        <TextBox IsReadOnly="True" Grid.Column="0" 
                                 Text="{Binding Text, UpdateSourceTrigger=PropertyChanged, RelativeSource={RelativeSource AncestorType={x:Type MultiSelectionComboBox}}}">
                        </TextBox>
                    </Border>
                    <Popup x:Name="PART_Popup" 
                               Grid.Column="0"
                               Focusable="False"
                               Grid.ColumnSpan="2"
                               IsOpen="{Binding IsDropDownOpen, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}"
                               Placement="Bottom"
                               VerticalOffset="-1"
                               PlacementTarget="{Binding ElementName=LayoutRoot}">
                        <Popup.Resources>
                            <Style TargetType="{x:Type ScrollBar}" BasedOn="{StaticResource {x:Type ScrollBar}}">
                                <Style.Triggers>
                                    <Trigger Property="Orientation" Value="Vertical">
                                        <Setter Property="BorderThickness" Value="0"/>
                                    </Trigger>
                                </Style.Triggers>
                            </Style>
                        </Popup.Resources>
                        <ScrollViewer x:Name="DropDownScrollViewer"
                                          Background="{StaticResource Background}"
                                          BorderBrush="{TemplateBinding BorderBrush}"
                                          BorderThickness="{TemplateBinding BorderThickness}"
                                          MinWidth="{Binding ActualWidth, ElementName=LayoutRoot}"
                                          MaxHeight="{TemplateBinding MaxDropDownHeight}">
                            <ItemsPresenter KeyboardNavigation.DirectionalNavigation="Contained"/>
                        </ScrollViewer>
                    </Popup>
                    <ToggleButton IsEnabled="{Binding IsEnabled, UpdateSourceTrigger=PropertyChanged, RelativeSource={RelativeSource AncestorType={x:Type MultiSelectionComboBox}}}" Grid.Column="1" IsChecked="{Binding IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" Style="{StaticResource ComboBoxToggleButton}"/>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
</ResourceDictionary>

但我无法获得via中复选框的引用,

this.ItemContainerGenerator.ContainerFromItem(this.Items[0]) as ComboBoxItem;

有什么建议吗?
我真正想做的是,
我想改变复选框ischecked属性,这是依赖于其他对象,可以改变运行时。我不能这样做,由于使用绑定的整体项目的当前状态,我不能改变在这一点上。所以基本上,一旦新的MultiSelectionComboBox被创建,我想做这样的事情,

foreach (object item in this.Items)
{
    ComboBoxItem comboBoxItem = this.ItemContainerGenerator.ContainerFromItem(item) as ComboBoxItem;
    if (comboBoxItem == null)
        continue;
    FrameworkElement element = comboBoxItem.ContentTemplate.LoadContent() as FrameworkElement;
    CheckBox checkBox = element.FindName("CheckBoxItem") as CheckBox;
    checkBox.IsChecked = this.SelectedItem.Contains(item);
}
e4eetjau

e4eetjau1#

尝试在this.ItemContainerGenerator.ContainerFromItem(item)之前执行UpdateLayout()

pb3skfrl

pb3skfrl2#

像这样使用ComboBox中的ItemContainerGenerator.StatusChanged事件:

myComboBox.ItemContainerGenerator.StatusChanged += ItemContainerGenerator_StatusChanged;

void ItemContainerGenerator_StatusChanged(object sender, System.EventArgs e)
{
    if (myComboBox.ItemContainerGenerator.Status == GeneratorStatus.ContainersGenerated)
    {
        foreach (var item in myComboBox.Items)
        {
            var container = (ComboBoxItem)LanguageComboBox.ItemContainerGenerator.ContainerFromItem(item);
        }
    }
}
dvtswwa3

dvtswwa33#

由于我的逻辑是在SelectionChanged事件中,我想知道为什么ItemContainerGenerator.ContainerFromItem方法总是返回null,即使Listbox.SelectedItem不是null,更奇怪的是,虚拟化被关闭了!看看ItemContainerGenerator.Status,我看到它是Primitives.GeneratorStatus.NotStarted,然后我在ItemContainerGenerator.Status == Primitives.GeneratorStatus.ContainersGenerated上添加了一个简单的测试,最终解决了这个问题,并且不需要subsribe到Status_Changed事件。

7uzetpgm

7uzetpgm4#

UpdateLayout(由R.Titov评论)为我解决了问题。我不需要更改虚拟化配置

相关问题