0

I want to use ItemsControl and bind datagrid rows to this, but it is not working and not draw items.

for example this code is working:

<GroupStyle.ContainerStyle>
    <Style TargetType="{x:Type GroupItem}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type GroupItem}">
                    <Expander x:Name="exp" Style="{DynamicResource ExpanderGroupStyle}" IsExpanded="{Binding Name.IsExpanded}">
                        <Expander.Header>
                            <TextBlock Text="{Binding Name.Name}" />
                        </Expander.Header>
                        <ItemsPresenter/>
                    </Expander>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</GroupStyle.ContainerStyle>

enter image description here

but this code is not working and drawing data this code is very fast. (I want to use VirtualizingStackPanel):

<GroupStyle.ContainerStyle>
    <Style TargetType="{x:Type GroupItem}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type GroupItem}">
                    <Expander x:Name="exp" Style="{DynamicResource ExpanderGroupStyle}" IsExpanded="{Binding Name.IsExpanded}">
                        <Expander.Header>
                            <TextBlock Text="{Binding Name.Name}" />
                        </Expander.Header>
                        <ItemsControl ItemsSource="{Binding Items}">
                            <ItemsControl.ItemsPanel>
                                <ItemsPanelTemplate>
                                    <VirtualizingStackPanel >
                                    </VirtualizingStackPanel>
                                </ItemsPanelTemplate>
                            </ItemsControl.ItemsPanel>
                            <ItemsControl.ItemTemplate>
                                <DataTemplate>
                                    <TextBlock Text="{Binding Mode=OneWay}"/>
                                    <!--<ItemsPresenter/>-->
                                </DataTemplate>
                            </ItemsControl.ItemTemplate>
                        </ItemsControl>
                    </Expander>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</GroupStyle.ContainerStyle>

enter image description here

if I use ItemsPresenter in code this is not draw data...

ekad
  • 14,436
  • 26
  • 44
  • 46
Ali Yousefi
  • 2,355
  • 2
  • 32
  • 47

1 Answers1

0

It seems like you need a scrollviewer to make VirtualizingStackpanel work. Please see Virtualizing an ItemsControl?

<ItemsControl
    VirtualizingStackPanel.IsVirtualizing="True"
    ScrollViewer.CanContentScroll="True">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <TextBlock
                Initialized="TextBlock_Initialized"
                Text="{Binding Path=Name}" />
        </DataTemplate>
    </ItemsControl.ItemTemplate>
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <VirtualizingStackPanel />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.Template>
        <ControlTemplate>
        <Border
            BorderThickness="{TemplateBinding Border.BorderThickness}"
            Padding="{TemplateBinding Control.Padding}"
            BorderBrush="{TemplateBinding Border.BorderBrush}"
            Background="{TemplateBinding Panel.Background}"
            SnapsToDevicePixels="True">
                <ScrollViewer
                    Padding="{TemplateBinding Control.Padding}"
                    Focusable="False">
                    <ItemsPresenter
                        SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" />
                </ScrollViewer>
            </Border>
            </ControlTemplate>
    </ItemsControl.Template>
</ItemsControl>
Community
  • 1
  • 1
Ash
  • 657
  • 9
  • 16
  • thanks for replay.this code is not working and not drawing DataGrid itemsSourceData (show me picture 2 result). If is use ItemsPresenter without ItemsControl. this is Show me Items and picture 1 render. – Ali Yousefi May 03 '15 at 12:57