5

我在这个问题上遇到了类似的问题,但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>

但我无法通过内部获得对复选框的引用,

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

有什么建议吗?

我真正想要实现的是,

我想更改 checkboxes 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);
}
4

3 回答 3

12

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

于 2014-02-21T12:27:25.753 回答
2

使用 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);
        }
    }
}
于 2014-04-03T09:38:15.040 回答
0

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

于 2016-08-08T15:52:31.593 回答