8,735
社区成员
发帖
与我相关
我的任务
分享
<Window x:Class="MyTestWpf.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MyTestWpf"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<local:MyTestConverter x:Key="testConverter"/>
</Window.Resources>
<Grid>
<DataGrid x:Name="dataGrid1" MinColumnWidth="80" AutoGenerateColumns="False" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Height="217" Width="425">
<DataGrid.Columns>
<DataGridTextColumn Header="名称" Binding="{Binding Key.Name}"/>
<DataGridTextColumn Header="内容" Binding="{Binding Key.Content}"/>
<DataGridTextColumn Header="时间" Binding="{Binding Key.Time,StringFormat='yyyy-MM-dd'}"/>
<DataGridTextColumn Header="报名人数" Binding="{Binding Value.Count}"/>
<DataGridTextColumn Header="参与人数" Binding="{Binding Value,Converter={StaticResource testConverter}}"/>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Data;
using System.Windows.Documents;
namespace MyTestWpf
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DateTime time = DateTime.Now;
Dictionary<Activity, List<ActivityEnroll>> source = new Dictionary<Activity, List<ActivityEnroll>>();
Activity a1 = new Activity {
Name = "名称 1",
Content = "内容 1",
Time = time.AddDays(-3)
};
List<ActivityEnroll> ae1 = new List<ActivityEnroll> {
new ActivityEnroll { Statr=1},
new ActivityEnroll { Statr=2},
new ActivityEnroll { Statr=1},
new ActivityEnroll { Statr=3},
new ActivityEnroll { Statr=0}
};
source.Add(a1, ae1);
Activity a2 = new Activity {
Name = "名称 2",
Content = "内容 2",
Time = time.AddDays(-5)
};
List<ActivityEnroll> ae2 = new List<ActivityEnroll> {
new ActivityEnroll { Statr=1},
new ActivityEnroll { Statr=2},
new ActivityEnroll { Statr=1},
new ActivityEnroll { Statr=1}
};
source.Add(a2, ae2);
var temp = source.ToList();
this.dataGrid1.ItemsSource = temp;
}
}
public class Activity
{
public String Name { get; set; }
public String Content { get; set; }
public DateTime Time { get; set; }
}
public class ActivityEnroll
{
public Int32 Statr { get; set; }
}
public class MyTestConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
List<ActivityEnroll> temp = value as List<ActivityEnroll>;
if (temp != null) {
return temp.Where(x => x.Statr == 1).ToList().Count;
}
return null;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
}