【发布时间】: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