MainPage.xaml
<Page
x:Class="UniversalAppTutorials.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:UniversalAppTutorials"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<StackPanel
x:Name="stack_panel1"
Margin="50"
Orientation="Vertical"
Background="AliceBlue"
Padding="50"
>
<ListView
x:Name="ListView1"
SelectionChanged="ListView1_SelectionChanged"
Loaded="ListView1_Loaded"
>
<ListViewItem>Red</ListViewItem>
<ListViewItem>Green</ListViewItem>
<ListViewItem>Blue</ListViewItem>
<ListViewItem>Black</ListViewItem>
<ListViewItem>Yellow</ListViewItem>
<ListViewItem>Orange</ListViewItem>
<ListViewItem>Olive</ListViewItem>
<ListViewItem>Crimson</ListViewItem>
</ListView>
</StackPanel>
</Page>
MainPage.xaml.cs
using Windows.UI.Xaml.Controls;
using Windows.UI.Popups;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Media;
using Windows.UI;
namespace UniversalAppTutorials
{
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
}
private void ListView1_Loaded(object sender, RoutedEventArgs e) {
// Get the instance of list view
ListView listView = sender as ListView;
// Get the ListView items collection
ItemCollection ic = listView.Items;
// Initialize a item counter
int counter = 1;
// Iterate through the list view items
foreach (ListViewItem item in ic) {
if (counter % 2 == 0)
{
// Change the item style
// Item background color
item.Background = new SolidColorBrush(Colors.Orange);
// Item text color
item.Foreground = new SolidColorBrush(Colors.DarkRed);
}
else {
// Change the alternate item style
// Alternate item background color
item.Background = new SolidColorBrush(Colors.OrangeRed);
// Alternate item text color
item.Foreground = new SolidColorBrush(Colors.Snow);
}
// Incremenet the counter by one
counter++;
}
}
private void ListView1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
// Get the instance of ListView
ListView listView = sender as ListView;
// Get the ListView selected item instance
ListViewItem item = (ListViewItem)listView.SelectedItem;
// Get the list view selected item text
string selectedText = (string) item.Content;
// Initialize a new message dialog
MessageDialog dialog = new MessageDialog("Selected : " + selectedText);
// Finally, display the selected item text on dialog
dialog.ShowAsync();
}
}
}



- UWP - SymbolIcon example
- UWP - How to add a new line to a TextBlock
- UWP - How to bold text in a TextBlock
- UWP - PivotItem header with image and text
- UWP - Simple Pivot example
- UWP - Polygon example
- UWP - Rectangle example
- UWP - Get ComboBox selected item
- UWP - ListView multi select example
- UWP - ListView item style example
- UWP - How to change ListView item height
- UWP - How to create a ListView programmatically
- UWP - ListView complex ItemsSource example
- UWP - ListView Array ItemsSource example
- UWP - How to use ListView