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