using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.IO;
using System.Collections.ObjectModel;
using System.ComponentModel;


namespace competition

...{

/**//// <summary>
/// Interaction logic for Window1.xaml
/// </summary>

public partial class Window1 : System.Windows.Window

...{

public Window1()

...{
InitializeComponent();
IniPlayer();
}

private PlayerList PlayIniList = new PlayerList();
private PlayerList PlayTempList = new PlayerList();
private ComPlayerList PlayerList = new ComPlayerList();
private List<Player> listTemp = new List<Player>();
private void OnLoaded(Object sender, EventArgs e)

...{
CompetitionTable.ItemsSource = PlayerList;
ScoreTable.ItemsSource = PlayIniList;

}

private void IniPlayer()

...{
System.Windows.Forms.OpenFileDialog openDlg = new System.Windows.Forms.OpenFileDialog();
openDlg.InitialDirectory = Environment.CurrentDirectory;
string filePath;
string[] players = null;
if (openDlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)

...{
filePath = openDlg.FileName;

StreamReader readLogFile = new StreamReader(filePath, Encoding.Default);
string readLogLine = string.Empty;
readLogLine = readLogFile.ReadLine();
players = readLogLine.Split(',');
for (int i = 0; i < 15; i++)

...{
Player p1 = new Player(players[2 * i]);
Player p2 = new Player(players[2 * i + 1]);

PlayerList.Add(new CompetitionPlayers(i + 1, p1, p2));
}
foreach (CompetitionPlayers player in PlayerList)

...{
PlayIniList.Add(player.Player1);
PlayIniList.Add(player.Player2);
listTemp.Add(new Player(player.Player1.Name, player.Player1.Score));
listTemp.Add(new Player(player.Player2.Name, player.Player2.Score));
}
}
else

...{
this.Close();
}
}

private void Sort()

...{
Player playerTempA;
Player playerTempB;
Player temp;

for (int i = 0; i < PlayIniList.Count; i++)

...{
playerTempA = PlayIniList[i] as Player;
temp = playerTempA;
if (playerTempA != null)

...{
for (int j = i + 1; j < PlayIniList.Count; j++)

...{
playerTempB = PlayIniList[j] as Player;

if (temp.Score < playerTempB.Score)

...{
PlayIniList[i] = playerTempB;
PlayIniList[j] = temp;
temp = PlayIniList[i];
}
else if (temp.Score == playerTempB.Score)

...{
int A = 0;
int B = 0;
bool bA = false;
bool bB = false;
foreach (CompetitionPlayers players in PlayerList)

...{
if (players.Player1.Name == temp.Name || players.Player2.Name == temp.Name)

...{
A = players.TableNum;
bA = true;
}
if (players.Player1.Name == playerTempB.Name || players.Player2.Name == playerTempB.Name)

...{
B = players.TableNum;
bB = true;
}
if (bA && bB)

...{
break;
}
}
if (A < B)

...{
PlayIniList[i] = playerTempB;
PlayIniList[j] = temp;
temp = PlayIniList[i];
}
}
}

}
}

PlayerList.Clear();
Player p1;
Player p2;
for (int i = 0; i < 30; i++)

...{
p1 = PlayIniList[i] as Player;
p2 = PlayIniList[i + 1] as Player;
if (p1 != null && p2 != null)

...{
PlayerList.Add(new CompetitionPlayers(i + 1, p1, p2));
}
i++;
}

}

private int m_Times = 0;

private void OnStartClick(Object sender, EventArgs e)

...{
Compete();
m_Times++;
BtnShow.IsEnabled = true;
BtnNext.IsEnabled = true;
BtnStart.IsEnabled = false;
}


private void OnShowClick(Object sender, EventArgs e)

...{
Sort();


ICollectionView dataView = CollectionViewSource.GetDefaultView(ScoreTable.ItemsSource);
if (dataView != null)

...{
dataView.Refresh();
}
BtnShow.IsEnabled = false;
BtnNext.IsEnabled = true;


}//ICollectionView

private void OnNextClick(Object sender, EventArgs e)

...{
if (m_Times < 7)

...{
Compete();
m_Times++;
}
BtnShow.IsEnabled = true;
BtnNext.IsEnabled = false;
if (m_Times == 7)

...{
BtnShow.IsEnabled = false;
BtnNext.IsEnabled = false;
}
}

private void Compete()

...{
if (PlayerList != null)

...{
Random rand = new Random();
foreach (CompetitionPlayers player in PlayerList)

...{
int r1 = rand.Next(100);
int r2 = rand.Next(100);
if (r1 > r2)

...{
player.Player1.Score++;
player.Winners = player.Player1;
}
else

...{
player.Player2.Score++;
player.Winners = player.Player2;
}
}
ICollectionView dataView = CollectionViewSource.GetDefaultView(CompetitionTable.ItemsSource);
if (dataView != null)

...{
dataView.Refresh();
}
}
}
}

class ComPlayerList : ObservableCollection<CompetitionPlayers>

...{
public ComPlayerList()

...{
}
}

class PlayerList : ObservableCollection<Player>

...{
public PlayerList()

...{
}
}
}
<Window x:Class="competition.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:src="clr-namespace:competition"
Title="competition" Height="600" Width="800" Loaded="OnLoaded"
>
<Window.Resources>
<src:ComPlayerList x:Key="PlayerDataList"/>
<src:ComPlayerList x:Key="PlayerList"/>

<DataTemplate x:Key="NumTemplate" DataType="{x:Type src:CompetitionPlayers}" >
<TextBlock Text="{Binding Path=TableNum}" HorizontalAlignment="Left" VerticalAlignment="Center"/>
</DataTemplate>
<DataTemplate x:Key="Player1Template" DataType="{x:Type src:CompetitionPlayers}" >
<TextBlock Text="{Binding Path=Player1Name}" HorizontalAlignment="Left" VerticalAlignment="Center"/>
</DataTemplate>
<DataTemplate x:Key="Player2Template" DataType="{x:Type src:CompetitionPlayers}" >
<TextBlock Text="{Binding Path=Player2Name}" HorizontalAlignment="Left" VerticalAlignment="Center"/>
</DataTemplate>
<DataTemplate x:Key="WinnersTemplate" DataType="{x:Type src:CompetitionPlayers}" >
<TextBlock Text="{Binding Path=WinnerName}" HorizontalAlignment="Left" VerticalAlignment="Center"/>
</DataTemplate>

<DataTemplate x:Key="ScoreTemplate" DataType="{x:Type src:Player}" >
<TextBlock Text="{Binding Path=Score}" HorizontalAlignment="Left" VerticalAlignment="Center"/>
</DataTemplate>
<DataTemplate x:Key="NameTemplate" DataType="{x:Type src:Player}" >
<TextBlock Text="{Binding Path=Name}" HorizontalAlignment="Left" VerticalAlignment="Center"/>
</DataTemplate>

</Window.Resources>

<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="300"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="500"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<!--ItemsSource="{Binding Source={StaticResource PlayerDataList}}"-->
<ListView Name="CompetitionTable" Width="450" Height="280" Margin="10,20,0,0" Grid.Row="0" Grid.Column="0" HorizontalAlignment="Left" VerticalAlignment="Top">
<ListView.View>
<GridView AllowsColumnReorder="False">
<GridViewColumn Header="TableNum" CellTemplate="{StaticResource NumTemplate}" Width="50"/>
<GridViewColumn Header="Player1" CellTemplate="{StaticResource Player1Template}" Width="100"/>
<GridViewColumn Header="Player2" CellTemplate="{StaticResource Player2Template}" Width="100" />
<GridViewColumn Header="Winner" CellTemplate="{StaticResource WinnersTemplate}" Width="100" />
</GridView>
</ListView.View>
</ListView>
<!--ItemsSource="{Binding Source={StaticResource PlayerList}}"-->
<ListView Name="ScoreTable" Grid.Row="0" Grid.Column="1" Margin="10,20,10,0" Width="200" Height="280" HorizontalAlignment="Left" VerticalAlignment="Top">
<ListView.View>
<GridView AllowsColumnReorder="False">
<GridViewColumn Header="Name" CellTemplate="{StaticResource NameTemplate}" Width="50"/>
<GridViewColumn Header="Score" CellTemplate="{StaticResource ScoreTemplate}" Width="50"/>
</GridView>
</ListView.View>
</ListView>

<Grid Grid.Row="1" Grid.Column="0" >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="200"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Button Width="50" Height="23" Grid.Column="0" Click="OnShowClick" Content="Show" Name="BtnShow" IsEnabled="False"/>
<Button Width="50" Height="23" Grid.Column="1" Click="OnStartClick" Content="Start" Name="BtnStart"/>
</Grid>
<Button Width="50" Height="23" Grid.Row="1" Grid.Column="1" Click="OnNextClick" Content="Next" Name="BtnNext" IsEnabled="False"/>
</Grid>
</Window>

ICollectionView:可以将List中的改变的内容更新到UI上面,很好用!
ICollectionView dataView = CollectionViewSource.GetDefaultView(CompetitionTable.ItemsSource);
if (dataView != null)
{
dataView.Refresh();
}
具体见sample
[读出文件中的比赛的人员名字显示在UI上面,然后进行7轮比赛,每次都将比分重新排序,然后显示出来,得分随机给出,每轮的比赛人员按照得分高低依次得出]