【问题标题】:How to not Expanding Grouping in ListView?如何不在 ListView 中扩展分组?
【发布时间】:2016-02-21 16:24:43
【问题描述】:

请问,我如何使用 DataTrigger 将 Grouping ListView 中的 Expander 设置为 False 并评估 ListView 中的任何列值?

我的代码:

XAML:

<ListView Name="lvwGrupoPunto">
    <ListView.View>
        <GridView>
            <GridView.ColumnHeaderContainerStyle>
                <Style TargetType="GridViewColumnHeader">
                    <Setter Property="Visibility" Value="Collapsed" />
                </Style>
            </GridView.ColumnHeaderContainerStyle>
            <GridViewColumn Header="Prestación" Width="300" DisplayMemberBinding="{Binding NombrePrestacion}"/>
            <GridViewColumn Header="Tipo Valor Punto" Width="30" DisplayMemberBinding="{Binding NombreTipoValorPunto}"/>
            <GridViewColumn Header="Valor Punto" Width="50" DisplayMemberBinding="{Binding ValorCalculoPunto, StringFormat=N2}"/>
        </GridView>
    </ListView.View>
    <ListView.GroupStyle>
        <GroupStyle>
            <GroupStyle.ContainerStyle>
                <Style TargetType="{x:Type GroupItem}">
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate>
                                <Expander>
                                    <Expander.Style>
                                        <Style TargetType="Expander">
                                            <Style.Triggers>
                                                <DataTrigger Binding="{Binding Path=ValorCalculoPunto}" Value="{x:Null}">
                                                    <Setter Property="IsExpanded" Value="False" />
                                                </DataTrigger>
                                            </Style.Triggers>
                                        </Style>
                                    </Expander.Style>
                                    <Expander.Header>
                                        <StackPanel Orientation="Horizontal">
                                            <TextBlock Text="{Binding Name}" FontWeight="Bold"/>
                                        </StackPanel>
                                    </Expander.Header>
                                    <ItemsPresenter />
                                </Expander>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </GroupStyle.ContainerStyle>
        </GroupStyle>
    </ListView.GroupStyle>
</ListView>

我的 C#:

    public class ItemValorPunto
    {
        public string NombrePrestacion { get; set; }
        public string NombreGrupoPunto { get; set; }    
        public decimal? ValorCalculoPunto { get; set; }
        public string TipoValorPunto { get; set; }
    }
    List<ItemValorPunto> itemValorPuntoColeccion = new List<ItemValorPunto>();
    itemValorPuntoColeccion.Add(new ItemValorPunto() { NombrePrestacion = "", NombreGrupoPunto = "PARTO 1", ValorCalculoPunto = null, TipoValorPunto = "" });
    itemValorPuntoColeccion.Add(new ItemValorPunto() { NombrePrestacion = "ANTICUERPOS ANTI SCL 70", NombreGrupoPunto = "PARTO 2", ValorCalculoPunto = 3.4000, TipoValorPunto = "$" });
    itemValorPuntoColeccion.Add(new ItemValorPunto() { NombrePrestacion = "ANTICUERPOS ANTI SM-RNP", NombreGrupoPunto = "PARTO 2", ValorCalculoPunto = 2.5000, TipoValorPunto = "%" });
    itemValorPuntoColeccion.Add(new ItemValorPunto() { NombrePrestacion = "", NombreGrupoPunto = "PARTO 3", ValorCalculoPunto = null, TipoValorPunto = "" });
    itemValorPuntoColeccion.Add(new ItemValorPunto() { NombrePrestacion = "", NombreGrupoPunto = "PARTO 4", ValorCalculoPunto = null, TipoValorPunto = "" });
    itemValorPuntoColeccion.Add(new ItemValorPunto() { NombrePrestacion = "ANTICUERPOS ANTI MITOCONDRIALE", NombreGrupoPunto = "PARTO 5", ValorCalculoPunto = 10.00, TipoValorPunto = "$" });
    itemValorPuntoColeccion.Add(new ItemValorPunto() { NombrePrestacion = "ANTICUERPOS ANTI SM-RNP", NombreGrupoPunto = "PARTO 5", ValorCalculoPunto = 0.10, TipoValorPunto = "%" });
    lvwGrupoPunto.ItemsSource = itemValorPuntoColeccion;
    CollectionView view = (CollectionView)CollectionViewSource.GetDefaultView(lvwGrupoPunto.ItemsSource);
    PropertyGroupDescription groupDescription = new PropertyGroupDescription("NombreGrupoPunto");
    view.GroupDescriptions.Add(groupDescription);

我只需要显示不为空“ValorCalculoPunto”的项目

But I get this

【问题讨论】:

    标签: wpf listview grouping expander


    【解决方案1】:

    将 XAML 中的 Expander 更改为:

    <Expander IsExpanded="{Binding Items[0].ValorCalculoPunto, Converter={StaticResource IsNotNullConverter}, Mode=OneWay}">
        <Expander.Header>
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding Name}" FontWeight="Bold"/>
            </StackPanel>
        </Expander.Header>
        <ItemsPresenter />
    </Expander>
    

    IsNotNullConverter 被添加到WindowsResources 集合中。

    <Window.Resources>
        <local:IsNotNullConverter x:Key="IsNotNullConverter"></local:IsNotNullConverter>
    </Window.Resources>
    

    它的实现是:

    public class IsNotNullConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return value != null;
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    

    问题在于默认情况下ExpanderIsExpanded 属性是false。所以你不能只用一个DataTrigger 告诉Expander 什么时候应该关闭。它们默认关闭。

    此外,当您在组中绑定DataContext 时,其类型为CollectionViewGroup,因此绑定中的Items[0] 部分。

    【讨论】:

    • 是的,我忘记设置 IsExpandable=True,但所有项目都是可见的。非常感谢,它运行良好,你救了一条命。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-04-21
    • 1970-01-01
    • 2018-11-04
    • 1970-01-01
    • 2012-12-02
    • 1970-01-01
    • 2017-04-30
    相关资源
    最近更新 更多