Step 1) Add .XAML page and Add List Box
Step 2) Create a Class , Named as Country
It has 2 properties 1.Code 2.Name
class Country
{
public String CountryName { get; set; }
public String CountryCode { get; set; }
}
Step 3) Add Sample data
void BuildCountries()
{
if (countryList.Count == 0)
{
countryList.Add(new Country { CountryCode="US",
CountryName="United States Of America" });
countryList.Add(new Country { CountryCode = "IN",
CountryName = "INDIA" });
countryList.Add(new Country { CountryCode = "UK",
CountryName = "United Kingdom" });
countryList.Add(new Country { CountryCode = "AU",
CountryName = "Australia" });
countryList.Add(new Country { CountryCode = "NZ",
CountryName = "NewZland" });
}
}
Step 4) Bind ListBox ItemSource with Country List
listbox1.ItemsSource = countryList;
Step 4) in XAML add Item Template to ListBox(Because for each item we need to display tooltip,so we need ItemTemplate)
<ListBox HorizontalAlignment="Left" Height="231" VerticalAlignment="Top" Width="240" x:Name="listbox1">
<ListBox.ItemTemplate>
<DataTemplate>
<!-- Each item is textblock so add TextBlock here and bind to CountryCode-->
<TextBlock Text="{Binding Path=CountryCode}">
<!-- TextBlock has tooltip property bind to CountryName -->
<TextBlock.ToolTip>
<ToolTip Content="{Binding Path=CountryName}"
Background="GreenYellow"
Foreground="White">
</ToolTip>
</TextBlock.ToolTip>
</TextBlock>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
That's it run the Application.
XAML
<Page x:Class="Wpfone.ListBox_Tooltip_Items"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300"
Title="ListBox_Tooltip_Items">
<Grid>
<ListBox BorderBrush="AliceBlue" BorderThickness="5" HorizontalAlignment="Left" Height="231" VerticalAlignment="Top" Width="240" x:Name="listbox1">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=CountryCode}">
<TextBlock.ToolTip>
<ToolTip Content="{Binding Path=CountryName}" Background="GreenYellow" Foreground="White"></ToolTip>
</TextBlock.ToolTip>
</TextBlock>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</Page>
.CS file
using System;
using System.Collections.Generic;
using System.Linq;
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.Navigation;
using System.Windows.Shapes;
namespace Wpfone
{
/// <summary>
/// Interaction logic for ListBox_Tooltip_Items.xaml
/// </summary>
public partial class ListBox_Tooltip_Items : Page
{
List<Country> countryList = new List<Country>();
public ListBox_Tooltip_Items()
{
InitializeComponent();
this.Loaded += ListBox_Tooltip_Items_Loaded;
}
void ListBox_Tooltip_Items_Loaded(object sender, RoutedEventArgs e)
{
BuildCountries();
BindtoListBox();
}
void BuildCountries()
{
if (countryList.Count == 0)
{
countryList.Add(new Country { CountryCode="US",
CountryName="United States Of America" });
countryList.Add(new Country { CountryCode = "IN",
CountryName = "INDIA" });
countryList.Add(new Country { CountryCode = "UK",
CountryName = "United Kingdom" });
countryList.Add(new Country { CountryCode = "AU",
CountryName = "Australia" });
countryList.Add(new Country { CountryCode = "NZ",
CountryName = "NewZland" });
}
}
void BindtoListBox()
{
try
{
listbox1.ItemsSource = countryList;
// listbox1.DisplayMemberPath = "CountryCode";
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
class Country
{
public String CountryName { get; set; }
public String CountryCode { get; set; }
}
}
Step 2) Create a Class , Named as Country
It has 2 properties 1.Code 2.Name
class Country
{
public String CountryName { get; set; }
public String CountryCode { get; set; }
}
Step 3) Add Sample data
void BuildCountries()
{
if (countryList.Count == 0)
{
countryList.Add(new Country { CountryCode="US",
CountryName="United States Of America" });
countryList.Add(new Country { CountryCode = "IN",
CountryName = "INDIA" });
countryList.Add(new Country { CountryCode = "UK",
CountryName = "United Kingdom" });
countryList.Add(new Country { CountryCode = "AU",
CountryName = "Australia" });
countryList.Add(new Country { CountryCode = "NZ",
CountryName = "NewZland" });
}
}
Step 4) Bind ListBox ItemSource with Country List
listbox1.ItemsSource = countryList;
Step 4) in XAML add Item Template to ListBox(Because for each item we need to display tooltip,so we need ItemTemplate)
<ListBox HorizontalAlignment="Left" Height="231" VerticalAlignment="Top" Width="240" x:Name="listbox1">
<ListBox.ItemTemplate>
<DataTemplate>
<!-- Each item is textblock so add TextBlock here and bind to CountryCode-->
<TextBlock Text="{Binding Path=CountryCode}">
<!-- TextBlock has tooltip property bind to CountryName -->
<TextBlock.ToolTip>
<ToolTip Content="{Binding Path=CountryName}"
Background="GreenYellow"
Foreground="White">
</ToolTip>
</TextBlock.ToolTip>
</TextBlock>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
That's it run the Application.
Source Code
XAML
<Page x:Class="Wpfone.ListBox_Tooltip_Items"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300"
Title="ListBox_Tooltip_Items">
<Grid>
<ListBox BorderBrush="AliceBlue" BorderThickness="5" HorizontalAlignment="Left" Height="231" VerticalAlignment="Top" Width="240" x:Name="listbox1">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=CountryCode}">
<TextBlock.ToolTip>
<ToolTip Content="{Binding Path=CountryName}" Background="GreenYellow" Foreground="White"></ToolTip>
</TextBlock.ToolTip>
</TextBlock>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</Page>
.CS file
using System;
using System.Collections.Generic;
using System.Linq;
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.Navigation;
using System.Windows.Shapes;
namespace Wpfone
{
/// <summary>
/// Interaction logic for ListBox_Tooltip_Items.xaml
/// </summary>
public partial class ListBox_Tooltip_Items : Page
{
List<Country> countryList = new List<Country>();
public ListBox_Tooltip_Items()
{
InitializeComponent();
this.Loaded += ListBox_Tooltip_Items_Loaded;
}
void ListBox_Tooltip_Items_Loaded(object sender, RoutedEventArgs e)
{
BuildCountries();
BindtoListBox();
}
void BuildCountries()
{
if (countryList.Count == 0)
{
countryList.Add(new Country { CountryCode="US",
CountryName="United States Of America" });
countryList.Add(new Country { CountryCode = "IN",
CountryName = "INDIA" });
countryList.Add(new Country { CountryCode = "UK",
CountryName = "United Kingdom" });
countryList.Add(new Country { CountryCode = "AU",
CountryName = "Australia" });
countryList.Add(new Country { CountryCode = "NZ",
CountryName = "NewZland" });
}
}
void BindtoListBox()
{
try
{
listbox1.ItemsSource = countryList;
// listbox1.DisplayMemberPath = "CountryCode";
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
class Country
{
public String CountryName { get; set; }
public String CountryCode { get; set; }
}
}
No comments:
Post a Comment