WPF-从组标题样式绑定到当前项目 [英] WPF - Binding to current item from within group header style

查看:67
本文介绍了WPF-从组标题样式绑定到当前项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有点像WPF菜鸟,所以请放轻松我;-)

I'm something of a WPF noob so please take it easy on me ;-)

我正在尝试创建一个分组的DataGrid(WPF工具包版本). 我已经成功创建了数据源,DataGrid本身,所需的CollectionViewSource和组头(使用扩展器)的样式.

I am trying to create a grouped DataGrid (WPF toolkit version). I have successfully created the data source, the DataGrid itself, the required CollectionViewSource and the Style for the group header (which uses an expander).

我想通过一个名为"Assign_To"的属性进行分组,并在标头中显示相关值(分组项目共享的值).但是,我不知道如何绑定到当前组/项目以返回其Assign_To属性.

I want to group by a property called 'Assign_To' and have the relevant value (the value that the grouped items share) show up in the header. However, I cannot work out how to bind to the current group/item in order to return its Assign_To property.

我得到的最接近(如下所示)的是绑定到整个CollectionViewSource,后者为Assign_To返回一个固定值.为返回"Assign_To"的正确值,绑定到当前项目/组的正确方法是什么?

The closest I have got (shown below) is binding to the overall CollectionViewSource, which returns a fixed value for Assign_To. What would be the proper way to bind to the current item/group in order to return the correct value for 'Assign_To'?

希望有人可以提供帮助.谢谢!

Hope someone can help. Thanks!

安迪(Andy T.)

Andy T.

这是来源...

<Window DataContext="{Binding Source={StaticResource SampleDataSource}}"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:dg="http://schemas.microsoft.com/wpf/2008/toolkit"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    x:Class="DataGridTest.MainWindow"
    x:Name="Window"
    Title="MainWindow"
    Width="640" Height="480" mc:Ignorable="d">

    <Window.Resources>
        <CollectionViewSource x:Key="CVS" Source="{Binding MyData}">
            <CollectionViewSource.GroupDescriptions>
                <PropertyGroupDescription PropertyName="Assign_To"/>
            </CollectionViewSource.GroupDescriptions>
        </CollectionViewSource>

        <Style x:Key="GroupHeaderStyle" TargetType="{x:Type GroupItem}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type GroupItem}">
                        <Expander IsExpanded="True">
                            <Expander.Header>
                                <StackPanel Orientation="Horizontal">
                                    <TextBlock Text="Assign To: "/>
                                    <TextBlock Text="{Binding Source={StaticResource CVS}, Path=Assign_To}"/>
                                </StackPanel>                               
                            </Expander.Header>
                            <ItemsPresenter/>
                        </Expander>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

    </Window.Resources>


    <Grid x:Name="LayoutRoot">
        <dg:DataGrid
         ItemsSource="{Binding Source={StaticResource CVS}}"
         SelectionUnit="CellOrRowHeader" 
         CanUserAddRows="False" 
         CanUserDeleteRows="False" 
         CanUserResizeRows="False">
         <dg:DataGrid.GroupStyle>
            <GroupStyle ContainerStyle="{StaticResource GroupHeaderStyle}">
                <GroupStyle.Panel>
                    <ItemsPanelTemplate>
                        <dg:DataGridRowsPresenter/>
                    </ItemsPanelTemplate>
                </GroupStyle.Panel>
            </GroupStyle>
         </dg:DataGrid.GroupStyle>          
        </dg:DataGrid>   
    </Grid> 
</Window>

推荐答案

感谢您的答复.我真的很感激,并会检查出来是否有效.

Thanks for your reply. I really appreciate it and will check it out to see if it works.

无论如何,事实证明,在进行一些戳戳和刺探之后,我仅使用XAML进行了求解.我所缺少的是,组标头绑定到的每个项目都是一个 GroupItem ,而 GroupItem 的默认DataContext是一个 CollectionViewGroup .反过来, CollectionViewGroup 具有一个 Items 属性,该属性是一个集合,因此我可以获取集合中第一项的 Assign_To 值并在我的标题文字中使用它.像这样:

Anyway, as it turns out, after some poking and prodding, I have worked it out using XAML only. What I had been missing was the fact that each item the group header is bound to is a GroupItem and that the default DataContext of a GroupItem is a CollectionViewGroup. In turn, a CollectionViewGroup has an Items property, which is a collection and I can therefore get the Assign_To value of the first item in the collection and use that in my header text. Like this:

<Style x:Key="GroupHeaderStyle" TargetType="{x:Type GroupItem}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type GroupItem}">
                        <Expander IsExpanded="True">
                            <Expander.Header>
                                <StackPanel Orientation="Horizontal">
                                    <TextBlock Text="Assign To: "/>
                                    <TextBlock Text="{Binding Items[0].Assign_To}"/>
                                </StackPanel>       
                            </Expander.Header>
                            <ItemsPresenter/>
                        </Expander>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

这篇关于WPF-从组标题样式绑定到当前项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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