You can try the code below:
The XAML code:
<ListBox Margin="0,136,1161,420">
<ListBoxItem Background="White" PointerEntered="ListBoxItem_PointerEntered" PointerExited="ListBoxItem_PointerExited"/>
<ListBoxItem Background="White" PointerEntered="ListBoxItem_PointerEntered" PointerExited="ListBoxItem_PointerExited"/>
</ListBox>
The cs code:
private void ListBoxItem_PointerEntered(object sender, PointerRoutedEventArgs e)
{
var listItem = sender as ListBoxItem;
listItem.Tag = listItem.Background;
listItem.Background = new SolidColorBrush(HexToColor("#336A5ACD"));
}
private void ListBoxItem_PointerExited(object sender, PointerRoutedEventArgs e)
{
var listItem = sender as ListBoxItem;
var initBackground = listItem.Tag as Brush;
if (initBackground != null)
{
listItem.Background = initBackground;
}
}
/// <summary>
/// The color from Hex string.
/// Only for this sample in this thread
/// </summary>
/// <param name="hexColor"></param>
/// <returns></returns>
private static Color HexToColor(string hexColor)
{
hexColor = hexColor.Replace("#", string.Empty);
byte alpha = byte.Parse(hexColor.Substring(0, 2), NumberStyles.AllowHexSpecifier);
byte red = byte.Parse(hexColor.Substring(2, 2), NumberStyles.AllowHexSpecifier);
byte green = byte.Parse(hexColor.Substring(4, 2), NumberStyles.AllowHexSpecifier);
byte blue = byte.Parse(hexColor.Substring(6, 2), NumberStyles.AllowHexSpecifier);
return Color.FromArgb(alpha, red, green, blue);
}
BR.
ShuaiHua Du
If right, please Accept.
Enjoy Programming!!!