【问题标题】:Expand Header binding don't show the value properly (WPF/C#)展开标题绑定未正确显示值(WPF/C#)
【发布时间】:2017-11-22 20:29:08
【问题描述】:

我正在使用数据绑定来显示listview 中的一些数据。我正在使用标题来汇总我的数据,并且我想显示从我的 itemsource 中提取的文本,但它不起作用。这部分代码有错误吗?如何为我的 wpf 提供正确的数据源?

在 Main.xml 中,我用 StructLog.cs 类填充列表,并在 wpf 上显示列表中每个项目的数据。其他值正常工作,并且创建了标题,只是没有出现标题扩展器上的文本。

Main.xml

    List<StructLog> all = new List<StructLog>();
    foreach (ObservableCollection<StructLog> res in Patterns.Results)
    {
        foreach (StructLog r in res)
        {
            all.Add(r);
        }
    }

    lstResults.ItemsSource = all;

    CollectionView view = (CollectionView)CollectionViewSource.GetDefaultView(all);
    PropertyGroupDescription groupDescription = new PropertyGroupDescription("Pattern");
    view.GroupDescriptions.Add(groupDescription);

StructLog.cs

public class StructLog
{
    public int LineNumber{ get; set;}
    public string LineLog{ get; set;}
    public DateTime Time{ get; set;}
    public string Source{ get; set;}
    public string Type{ get; set;}
    public string Pattern{ get; set;}

    public StructLog(StructLine s,string patternName)
    {
        this.LineNumber = s.LineNumber;
        this.LineLog = s.LineLog;
        this.Time = s.Time;
        this.Source = s.Source;
        this.Type = s.Type;
        this.Pattern = patternName;
    }

}

Window.xaml

<ListView Name="lstResults" Grid.Row="1" IsEnabled="True" Grid.RowSpan="4" DataContext="Results" Grid.ColumnSpan="5" Margin="5,5">
    <ListView.View>
        <GridView>
            <GridViewColumn Header="Linha" Width="Auto" DisplayMemberBinding="{Binding LineNumber}" />
            <GridViewColumn Header="Fonte" Width="Auto" DisplayMemberBinding="{Binding Source}" />
            <GridViewColumn Header="Data" Width="Auto" DisplayMemberBinding="{Binding Time}" />
            <GridViewColumn Header="Log" Width="Auto" DisplayMemberBinding="{Binding LineLog}" />
        </GridView>
    </ListView.View>

    <ListView.GroupStyle>
        <GroupStyle>
            <GroupStyle.ContainerStyle>
                <Style TargetType="{x:Type GroupItem}">
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate>
                                <Expander IsExpanded="True">
                                    <Expander.Header>
                                        <StackPanel Orientation="Horizontal">
                                            <TextBlock Text="Teste:" FontWeight="Bold" Foreground="Gray" FontSize="22" VerticalAlignment="Bottom" />
                                            <TextBlock Text="{Binding Pattern}" FontWeight="Bold" Foreground="Gray" FontSize="22" VerticalAlignment="Bottom" />
                                         </StackPanel>
                                    </Expander.Header>
                                    <ItemsPresenter />
                                </Expander>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </GroupStyle.ContainerStyle>
        </GroupStyle>
    </ListView.GroupStyle>
</ListView>

【问题讨论】:

    标签: c# wpf xaml listview data-binding


    【解决方案1】:

    为了在组标题中显示一个值,分组项目将在 GroupItem 类的 Items 属性中。所以你可以像 Text ="{Binding Items[0].Pattern }" 那样进行绑定。这将绑定组中第一项的值,因为组中的所有值都将相似,因为这些值是按 Pattern 属性分组的。

    试试这个。

     <ListView.GroupStyle>
        <GroupStyle>
            <GroupStyle.ContainerStyle>
                <Style TargetType="{x:Type GroupItem}">
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate>
                                <Expander IsExpanded="True">
                                    <Expander.Header>
                                        <StackPanel Orientation="Horizontal">
                                            <TextBlock Text="Teste:" FontWeight="Bold" Foreground="Gray" FontSize="22" VerticalAlignment="Bottom" />
                                            <TextBlock Text="{Binding Items[0].Pattern}" FontWeight="Bold" Foreground="Gray" FontSize="22" VerticalAlignment="Bottom" />
                                         </StackPanel>
                                    </Expander.Header>
                                    <ItemsPresenter />
                                </Expander>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </GroupStyle.ContainerStyle>
        </GroupStyle>
    </ListView.GroupStyle>
    

    【讨论】:

      猜你喜欢
      • 2017-07-31
      • 2011-06-06
      • 1970-01-01
      • 2012-06-26
      • 2011-02-09
      • 2023-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多