wpf datagrid自动扩展第一组 [英] wpf datagrid automatically expand first group

查看:183
本文介绍了wpf datagrid自动扩展第一组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个datagrid,其中itemsource绑定到一个ListCollectionView与一个组。
当我填写这个集合时,我希望第一个组自动地被视为扩展,如何在wpf(codebehind或mvvm)中编码?

 
ItemsSource ={Binding ResultColl}
SelectedItem ={Binding Path = SelectedResultItem,Mode = TwoWay}
SelectionMode =SingleIsReadOnly = True>
< DataGrid.GroupStyle>
< GroupStyle>
< GroupStyle.ContainerStyle>
< Style TargetType ={x:Type GroupItem}>
< Setter Property =Template>
< Setter.Value>
< ControlTemplate TargetType ={x:Type GroupItem}>
< Expander>
< Expander.Header>
< StackPanel>
< TextBox Text ={Binding Items [0] .ID}/>
< / StackPanel>
< /Expander.Header>
< ItemsPresenter />
< / Expander>
< / ControlTemplate>
< /Setter.Value>
< / Setter>
< / Style>
< /GroupStyle.ContainerStyle>
< / GroupStyle>
< /DataGrid.GroupStyle>

< DataGrid.Columns>
< DataGridTextColumn Binding ={Binding Path = ID}/>
< DataGridTextColumn Binding ={Binding Path = Typ}/>
< DataGridTextColumn Binding ={Binding Path = Info}/>
< DataGridTextColumn Binding ={Binding Path = orderDate,StringFormat = {} {0:dd-MM-yyyy}}/>
< /DataGrid.Columns>
< / DataGrid>

在mvvm控制器中:

  ListCollectionView tmp = new ListCollectionView(myList); 
tmp.GroupDescriptions.Add(new PropertyGroupDescription(ID));
ResultColl = tmp;
...
ListCollectionView _resultColl;
public ListCollectionView ResultColl
{
get {return _resultColl; }
set {_resultColl = value;

RaisePropertyChanged(ResultColl);
if(value!= null&& _resultColl.Count> 0)
SelectedResultItem = _resultColl.GetItemAt(0)as ItemResult;
}
}

执行代码时,数据网格填充第1

解决方案

将IsExpanded属性添加到您的类中,并将绑定添加到Expander:

 < Expander IsExpanded ={Binding Items [0] .IsExpanded}> 

设置IsExpanded为第一个为true


I have a datagrid with the itemsource bound to a ListCollectionView with one group. When i fill the collection, i want the first group autmatically viewed as expanded, how to code that in wpf (codebehind or mvvm)?

            <DataGrid 
                 ItemsSource="{Binding ResultColl}" 
                 SelectedItem="{Binding Path=SelectedResultItem, Mode=TwoWay}"
                 SelectionMode="Single" IsReadOnly="True" >
            <DataGrid.GroupStyle>
                <GroupStyle>
                    <GroupStyle.ContainerStyle>
                        <Style TargetType="{x:Type GroupItem}">
                            <Setter Property="Template">
                                <Setter.Value>
                                    <ControlTemplate TargetType="{x:Type GroupItem}">
                                        <Expander>
                                            <Expander.Header>
                                                <StackPanel>
                                                        <TextBox Text="{Binding Items[0].ID}" />
                                                </StackPanel>
                                            </Expander.Header>
                                            <ItemsPresenter />
                                        </Expander>
                                    </ControlTemplate>
                                </Setter.Value>
                            </Setter>
                        </Style>
                    </GroupStyle.ContainerStyle>
                </GroupStyle>
            </DataGrid.GroupStyle>

                <DataGrid.Columns>
                    <DataGridTextColumn Binding="{Binding Path=ID}"/>
                    <DataGridTextColumn Binding="{Binding Path=Typ}"/>
                    <DataGridTextColumn Binding="{Binding Path=Info}"/>
                    <DataGridTextColumn Binding="{Binding Path=orderDate, StringFormat={}{0:dd-MM-yyyy}}"/>
                </DataGrid.Columns>
            </DataGrid>

In the mvvm controller:

    ListCollectionView tmp = new ListCollectionView(myList);
    tmp.GroupDescriptions.Add(new PropertyGroupDescription("ID"));
    ResultColl = tmp;
    ...
    ListCollectionView _resultColl;
    public ListCollectionView ResultColl
    {
        get { return _resultColl; }
        set { _resultColl = value;

            RaisePropertyChanged("ResultColl");
            if (value != null && _resultColl.Count > 0)
                SelectedResultItem = _resultColl.GetItemAt(0) as ItemResult;
        }
    }

When executing the code, the datagrid is filled the 1st item is selected but group is collapsed.

解决方案

Add IsExpanded property to your class and add binding to Expander:

<Expander IsExpanded="{Binding Items[0].IsExpanded}">

Set IsExpanded for first to true

这篇关于wpf datagrid自动扩展第一组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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