I don't know how to implement functions to my XAML
<Window x:Class="WpfApplicationLAB.HighScore"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplicationLAB"
xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase"
mc:Ignorable="d"
Title="HighScore" Height="300" Width="300">
<Window.Resources>
<CollectionViewSource x:Key="SortedItems" Source="{Binding items}">
<CollectionViewSource.SortDescriptions>
<scm:SortDescription PropertyName="Result"/>
<scm:SortDescription PropertyName="Name"/>
</CollectionViewSource.SortDescriptions>
</CollectionViewSource>
</Window.Resources>
<Grid>
<ListView Margin="10" Name="lvDataBinding" ItemsSource ="{Binding items}" >
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="Height" Value="40" />
</Style>
</ListView.ItemContainerStyle>
<ListView.ItemTemplate>
<DataTemplate>
<WrapPanel>
<TextBlock Text="{Binding Name}" FontWeight="Bold" />
<TextBlock Text=" " />
<TextBlock Text="{Binding Result}" FontWeight="Bold" />
</WrapPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
</Window>
and C#
public partial class HighScore : Window
{
public HighScore()
{
InitializeComponent();
List<User> items = new List<User>();
items.Add(new User() { Name = "John Doe", Result = 42 });
items.Add(new User() { Name = "Jane Doe", Result = 39 });
items.Add(new User() { Name = "Sammy Doe", Result = 13 });
}
}
public class User
{
public string Name { get; set; }
public int Result { get; set; }
public override string ToString()
{
return this.Name + " " + this.Result;
}
}
}
Tried to implement CollectionView
sorting but it's not working, what should I change?
Second thing is that I wanted to alternate background color and font color. I tried also to implement function for alternating from this example: Alternate background color in Listview XAML. But I don't understand Property="ItemsControl.AlternationIndex" Value="0"
and
<DataTemplate DataType="system:String">
<!-- put your data template here -->
</DataTemplate>
How should I implement this?