wp7:突出显示列表框中选定的图像

发布于 2024-12-07 18:17:13 字数 100 浏览 1 评论 0原文

我有一个列表框,在其中显示图标列表。我想通过将图标颜色从“白色”更改为“蓝色”来突出显示所选项目。这对我来说听起来很简单,但似乎非常困难。

有人对采取的最佳方法有建议吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

美人如玉 2024-12-14 18:17:13

您想要更改图标的实际颜色或突出显示 ListBox 中的所选项目吗?如果是后者,则添加一个 SelectionChanged 事件处理程序。在此处理程序中,执行以下操作:

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 );

如果您希望重置先前所选项目的 BorderBrush,请查看 SelectionChangedEventArgs.RemovedItems 财产。您可以使用类似于我发布的代码来重置颜色。

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.

饮惑 2024-12-14 18:17:13

对于第一种情况,您需要创建两个图标图像,一个用于选定的图标图像,另一个用于普通视图。
您可以在选择更改事件时更改列表框中的图像,如下所示

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;
        }

    }

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;
        }

    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文