在datagrid中使用组时,列折叠 [英] Columns are collapsed when using group in datagrid

查看:165
本文介绍了在datagrid中使用组时,列折叠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个datagrid,其中显示几个对象与一些列。我添加了分组到datagrid,现在看起来它不像宽度为'*'的列表 - 列都被折叠到最小宽度。但是,当我刷新网格(添加元素或修改现有元素后),我可以看到列刷新。
奇怪的是,我调用相同的函数两次,第一次它不工作,而第二次它的作用。



之前:



http://www.microage-dil.ca/SO/ GridBefore.png



更新一次



http://www.microage-dil.ca/SO/GridAfter.png
这是刷新功能:

  Private Sub ListerDocuments()
Dim lstCVDocuments As New ListCollectionView(_oLstDocuments)
lstCVDocuments.GroupDescriptions.Add(New PropertyGroupDescription( TypeFichier))

GridDocuments.ItemsSource = lstCVDocuments
GridDocuments.Items.Refresh()
End Sub

现在datagrid:

 < DataGrid x:Name = GridDocumentsGrid.Column =0ItemsSource ={Binding}Style ={StaticResource BaseGrid}IsTabStop =False> 
< DataGrid.GroupStyle>
< GroupStyle>
< GroupStyle.HeaderTemplate>
< DataTemplate>
< StackPanel Orientation =Horizo​​ntalHorizo​​ntalAlignment =Stretch>
< TextBlock Text ={Binding Path = Name}/>
< / StackPanel>
< / DataTemplate>
< /GroupStyle.HeaderTemplate>
< GroupStyle.ContainerStyle>
< Style TargetType ={x:Type GroupItem}>
< Setter Property =Template>
< Setter.Value>
< ControlTemplate TargetType ={x:Type GroupItem}>
< Expander>
< Expander.Header>
< StackPanel Orientation =Horizo​​ntalHorizo​​ntalAlignment =Stretch>
< TextBlock Text ={Binding Path = Name}FontWeight =Bold/>
< / StackPanel>
< /Expander.Header>
< ItemsPresenter />
< / Expander>
< / ControlTemplate>
< /Setter.Value>
< / Setter>
< / Style>
< /GroupStyle.ContainerStyle>
< / GroupStyle>
< /DataGrid.GroupStyle>
< DataGrid.Columns>
< DataGridTextColumn Header =NomWidth =2 *MinWidth =150
Binding ={Binding NomFichier}
IsReadOnly =True/>
< DataGridTextColumn Header =FichierWidth =3 *MinWidth =150
Binding ={Binding NomFichierOriginal}
IsReadOnly =True/>
< DataGridTextColumn Header =ExtensionWidth =65MinWidth =50
Binding ={Binding ExtensionFormate}
IsReadOnly =True/>
< DataGridTextColumn Header =Date d'ajoutWidth =80MinWidth =80
Binding ={Binding DateAjout,StringFormat = yyyy-MM-dd}
IsReadOnly =True/>
< /DataGrid.Columns>
< / DataGrid>

我在网上得到了分组,似乎工作正常,直到我发现了一个特别的错误。请注意,我添加了Horizo​​ntalAlignment =Strech,因为我虽然可能会解决问题,但是它并没有解决。



编辑:忘了提一下,我第一次打电话ListerDocument函数,该变量完全加载文档,并在构造函数中调用(New)

解决方案

尝试指定您的DataGrid中的GroupStyle.Panel。原因是默认的GroupStyle.Panel(StackPanel)不适合DataGrid宽度*,列将会崩溃。

 < GroupStyle.Panel> 
< ItemsPanelTemplate>
< DataGridRowsPresenter />
< / ItemsPanelTemplate>
< /GroupStyle.Panel>


I have a datagrid in which I display several objects with some columns. I added grouping to the datagrid and now it seems that it doens't like the colulmns with width of '*' - the columns are all collapsed to their minimum widths. However, when I refresh the grid (after adding an element or modifiying an existing one), I can see the columns refreshing alright. The weird thing is that I call the same function twice and the first time it doesn't work while the second it does.

Before :

http://www.microage-dil.ca/SO/GridBefore.png

After updating once

http://www.microage-dil.ca/SO/GridAfter.png Here is the Refresh function :

Private Sub ListerDocuments()
    Dim lstCVDocuments As New ListCollectionView(_oLstDocuments)
    lstCVDocuments.GroupDescriptions.Add(New PropertyGroupDescription("TypeFichier"))

    GridDocuments.ItemsSource = lstCVDocuments
    GridDocuments.Items.Refresh()
End Sub

And now the datagrid :

<DataGrid x:Name="GridDocuments" Grid.Column="0" ItemsSource="{Binding}" Style="{StaticResource BaseGrid}"  IsTabStop="False">
    <DataGrid.GroupStyle>
        <GroupStyle>
            <GroupStyle.HeaderTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal" HorizontalAlignment="Stretch">
                        <TextBlock Text="{Binding Path=Name}" />
                    </StackPanel>
                </DataTemplate>
            </GroupStyle.HeaderTemplate>
            <GroupStyle.ContainerStyle>
                <Style TargetType="{x:Type GroupItem}">
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type GroupItem}">
                                <Expander>
                                    <Expander.Header>
                                        <StackPanel Orientation="Horizontal"  HorizontalAlignment="Stretch">
                                            <TextBlock Text="{Binding Path=Name}" FontWeight="Bold" />
                                        </StackPanel>
                                    </Expander.Header>
                                    <ItemsPresenter />
                                </Expander>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </GroupStyle.ContainerStyle>
        </GroupStyle>
    </DataGrid.GroupStyle>
    <DataGrid.Columns>                            
        <DataGridTextColumn Header="Nom" Width="2*"  MinWidth="150"
                            Binding="{Binding NomFichier}"                                               
                            IsReadOnly="True" />
        <DataGridTextColumn Header="Fichier" Width="3*"  MinWidth="150"
                            Binding="{Binding NomFichierOriginal}"                                               
                            IsReadOnly="True" />
        <DataGridTextColumn Header="Extension" Width="65"  MinWidth="50"
                            Binding="{Binding ExtensionFormate}"                                               
                            IsReadOnly="True" />
        <DataGridTextColumn Header="Date d'ajout" Width="80"  MinWidth="80"
                            Binding="{Binding  DateAjout, StringFormat=yyyy-MM-dd}"                                               
                            IsReadOnly="True" />
    </DataGrid.Columns>                        
</DataGrid>

I got the grouping part on the net and it seemed to work fine until I found tha tparticular bug. Note that I added the HorizontalAlignment ="Strech" because I though it might solve the problem but obviusly it did not.

EDIT : Forgot to mention, the first time I call the ListerDocument function, the variable is fully loaded with the documents and it is called in the constructor (New)

解决方案

Try to specify the GroupStyle.Panel in your DataGrid. The reason for this is the fact that the default GroupStyle.Panel (StackPanel) does not fit for the DataGrid Width "*" and the columns will collapse.

<GroupStyle.Panel>
  <ItemsPanelTemplate>
    <DataGridRowsPresenter/>
  </ItemsPanelTemplate>
</GroupStyle.Panel>

这篇关于在datagrid中使用组时,列折叠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆