【问题标题】:wpf datagrid automatically expand first groupwpf datagrid 自动展开第一组
【发布时间】:2012-12-28 08:36:15
【问题描述】:

我有一个数据网格,其中 itemsource 绑定到一组 ListCollectionView。 当我填充集合时,我希望自动将第一组视为扩展,如何在 wpf(代码隐藏或 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>

在 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;
    }
}

执行代码时,数据网格被填充,第一个项目被选中,但组被折叠。

【问题讨论】:

  • 这个太模糊了,到目前为止你尝试过什么?
  • 我在设置collectionview后尝试设置SelectedItem属性

标签: wpf datagrid expand


【解决方案1】:

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

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

将 IsExpanded for first 设置为 true

【讨论】:

  • 好的,我想知道它是否只能在 xaml 代码中完成。
  • 谢谢!一旦你弄清楚它看起来很明显。
【解决方案2】:

您可以尝试将另一个 bool 属性添加到您的视图模型中,默认为 true,但在第一次使用时切换为 false。并使用 OneTime 模式将 Expander 的 IsExpanded 属性绑定到此。

    public bool IsExpanded
    {
        get
        {
            if (_isExpanded)
            {
                _isExpanded = false;
                return true;
            }
            return false;
        }
    }

Xaml 会是这样的:

<Expander IsExpanded="{Binding DataContext.IsExpanded, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}, Mode=OneTime}">

【讨论】:

    猜你喜欢
    • 2012-07-24
    • 1970-01-01
    • 2012-12-09
    • 1970-01-01
    • 2019-04-16
    • 1970-01-01
    • 1970-01-01
    • 2012-11-17
    • 2011-01-10
    相关资源
    最近更新 更多