开发者

wp7: highlight selected image within a listbox

I have a list box where I'm displaying a list of icons. I want to highlight the selected item by changing the icon color from 'white' to 'blue'. This sounds simple to me, 开发者_如何学编程but it seems to be very difficult.

Does anyone have suggestions on the best approach to take?


Do you want to change the actual color of the icon or highlight the selected item in the ListBox? If it is the latter, then add a SelectionChanged event handler. Within this handler do the following:

var lb = sender as ListBox;
var lbi = lb.ItemContainerGenerator.ContainerFromItem(lb.SelectedItem) as ListBoxItem;

lbi.BorderBrush = new SolidColorBrush( Colors.Blue );
// or
lbi.Background = new SolidColorBrush( Colors.Blue );

If you wish to reset the BorderBrush for the previously selected item, take a look at the SelectionChangedEventArgs.RemovedItems property. You can use code similar to what I've posted to reset the color.


For first case you need to create two icon images one for selected and another of normal view. you can change image in list box on selection change event as bellow

private void listBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        string imgUri = "selectedImageName.png";

        BitmapImage bmp = new BitmapImage();
        bmp.UriSource = new Uri(imgUri, UriKind.Relative);

        (listBox1.SelectedItem as Image).Source = bmp;

       // for resetting unselected items  
       BitmapImage bmp1 = new BitmapImage();
        foreach (var v in e.RemovedItems)
        {
            imgUri = "imageNameForNormalView.png";

            bmp1.UriSource = new Uri(imgUri, UriKind.Relative);
            (v as Image).Source = bmp1;
        }

    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜