本文介绍了ItemContainerGenerator.ContainerFromItem()返回null而VirtualizingStackPanel.IsVirtualizing ="假"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我面临着 VirtualizingStackPanel.IsVirtualizing =FALSE并没有解决我的问题。是否有任何人面临同样的问题?



的事情是我有一个自定义组合框,

 <风格的TargetType ={X:类型MultiSelectionComboBox}> 
< setter属性=VerticalContentAlignmentVALUE =中心/>
< setter属性=ItemsPanel>
< Setter.Value>
< ItemsPanelTemplate>
< StackPanel的方向=垂直
VerticalAlignment =中心
的Horizo​​ntalAlignment =中心
VirtualizingStackPanel.IsVirtualizing =FALSE/>
< / ItemsPanelTemplate>
< /Setter.Value>
< /二传手>
< setter属性=ItemTemplate中>
< Setter.Value>
<&DataTemplate的GT;
< StackPanel的方向=横向X:NAME =ItemStackVirtualizingStackPanel.IsVirtualizing =FALSE>
<复选框X:NAME =CheckBoxItem
命令={结合的SelectItem,的RelativeSource = {的RelativeSource AncestorType = {X:类型MultiSelectionComboBox}}}
CommandParameter ={结合重点}
>
< /复选框>
< TextBlock的文本={结合DisplayText}>< / TextBlock的>
< / StackPanel的>
< / DataTemplate中>
< /Setter.Value>
< /二传手>
< setter属性=模板>
< Setter.Value>
<的ControlTemplate的TargetType ={X:类型组合框}>
<电网X:NAME =放置SnapsToDevicePixels =真>
< Grid.ColumnDefinitions>
< ColumnDefinition WIDTH =*/>
< ColumnDefinition WIDTH =自动/>
< /Grid.ColumnDefinitions>
< BORDER了borderThickness =1BorderBrush =黑>
<文本框IsReadOnly =真Grid.Column =0
文本={结合文字,UpdateSourceTrigger = PropertyChanged的,的RelativeSource = {的RelativeSource AncestorType = {X:类型MultiSelectionComboBox}}}> ;
< /文本框>
< /边框>
将;弹出X:名称=PART_Popup
Grid.Column =0
调焦=假
Grid.ColumnSpan =2
ISOPEN ={结合IsDropDownOpen,的RelativeSource = {的RelativeSource TemplatedParent},模式=双向}
放置=底
VerticalOffset = - 1
PlacementTarget ={绑定的ElementName = LayoutRoot} >
< Popup.Resources>
<风格的TargetType ={X:类型滚动条}支持算法FMP ={StaticResource的{X:类型滚动条}}>
< Style.Triggers>
<触发属性=方向值=垂直>
< setter属性=了borderThicknessVALUE =0/>
< /触发>
< /Style.Triggers>
< /样式和GT;
< /Popup.Resources>
<的ScrollViewer X:NAME =DropDownScrollViewer
背景={S​​taticResource的背景}
BorderBrush ={TemplateBinding BorderBrush}
了borderThickness ={TemplateBinding了borderThickness}
了minWidth ={结合ActualWidth的,的ElementName = LayoutRoot}
了maxHeight ={TemplateBinding MaxDropDownHeight}>
< ItemsPresenter KeyboardNavigation.DirectionalNavigation =含有/>
< /&的ScrollViewer GT;
< /弹出>
<切换按钮IsEnabled ={结合IsEnabled,UpdateSourceTrigger = PropertyChanged的,的RelativeSource = {的RelativeSource AncestorType = {X:类型MultiSelectionComboBox}}}Grid.Column =1=器isChecked{结合IsDropDownOpen,模式=双向,的RelativeSource = {的RelativeSource TemplatedParent}}风格={StaticResource的ComboBoxToggleButton}/>
< /网格和GT;
< /控件模板>
< /Setter.Value>
< /二传手>
< /样式和GT;
< / ResourceDictionary的>



,但我无法通过,

$ B获得里面的复选框参考
$ b

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

有什么建议吗?



什么其实我是想实现的是,



我想改变复选框这是一个不同的物件可以在运行时改变物业器isChecked。我不能用,由于我不能在这一点上改变整个项目的当前状态绑定做到这一点。所以基本上,一旦新MultiSelectionComboBox创建我想要做这样的事情,

 的foreach(对象this.Items项)
{
ComboBoxItem comboBoxItem = this.ItemContainerGenerator.ContainerFromItem(项目)为ComboBoxItem;
如果(comboBoxItem == NULL)
继续;
FrameworkElement的元素= comboBoxItem.ContentTemplate.LoadContent()作为FrameworkElement的;
复选框复选框= element.FindName(CheckBoxItem)的复选框;
checkBox.IsChecked = this.SelectedItem.Contains(项目);
}


解决方案

尝试执行 UpdateLayout请() this.ItemContainerGenerator.ContainerFromItem(项目)


I'm facing a similar problem with this question however VirtualizingStackPanel.IsVirtualizing="False" didn't solve my problem. Is there anyone facing the same issue?

The thing is I have a custom combobox,

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

and yet I can't get a reference to the checkbox inside via,

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

Is there any suggestions?

What i actually want to achieve is,

i want to change checkboxes ischecked property which is depending on an other object which can change on runtime. I can't do it with using bindings due to the current state of the overall project which i can not change at this point. So basically once the new MultiSelectionComboBox is created i want to do something like this,

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);
}
解决方案

try execute UpdateLayout() before this.ItemContainerGenerator.ContainerFromItem(item)

这篇关于ItemContainerGenerator.ContainerFromItem()返回null而VirtualizingStackPanel.IsVirtualizing =&QUOT;假&QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-17 09:07