.net c#每个列表框项目的背景不同

Different background on each Listbox item
2021-03-07
  •  译文(汉语)
  •  原文(英语)

我有一个列表框,其中包含5个列(idStory,标题,创建的,textStory和feel)的项目绑定在我的表上.

<ListBox x:Name="MainListBox" Height="418" SelectionChanged="MainListBox_SelectionChanged">
<ListBox.ItemTemplate>
    <DataTemplate>
        <StackPanel x:Name="listPanel" Orientation="Horizontal" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="0,5,0,0">
            <Rectangle Width="100" Height="100" Fill="#e34d64" />
            <StackPanel Orientation="Vertical" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="15,0,0,0">
                <TextBlock Text="{Binding Title}" FontSize="26" Foreground="Black"/>
                <TextBlock Text="{Binding Created}" FontSize="20" Foreground="Black"/>
                <TextBlock Text="{Binding TextStory}" FontSize="20" Foreground="Black" FontStyle="Italic"/>
            </StackPanel>
            <toolkit:ContextMenuService.ContextMenu>
                <toolkit:ContextMenu>
                    <toolkit:MenuItem x:Name="menuEdit" Header="Edit Story" Click="menuEdit_Click"/>
                    <toolkit:MenuItem x:Name="menuDelete" Header="Delete Story" Click="menuDelete_Click"/>
                </toolkit:ContextMenu>
            </toolkit:ContextMenuService.ContextMenu>
        </StackPanel>
    </DataTemplate>
</ListBox.ItemTemplate>

它运作良好,但我想根据列的感觉更改每个项目的背景.例如,如果我们得到字符串"sad",则列表框项将具有蓝色作为背景,例如http://imgur.com/n5LoNgj

我应该怎么做才能使其成为现实?任何帮助将不胜感激.谢谢.

速聊1:
我已从问题标题中删除了标签.请注意,在大多数情况下,问题的标题中不应包含标签.
速聊2:
哦,我很抱歉.谢谢
速聊3:
如果标记是按照本问题中的说明灵活地集成的,则标题中的标记很好(请参见链接中的第二个示例).
解决过程1

出于您的目的,我建议使用Converter将您的TextBox的Background(或者更好的是整个StackPanel-您的选择)绑定到Feeling属性:

<TextBlock Text="{Binding Title}" FontSize="26" Foreground="Black" Background={Binding Feeling, Converter={StaticResource TxtToBrush}}/>

您将必须在资源中的某个位置向您的Converter添加密钥:

xmlns:conv="clr-namespace:Yournamespace"
<conv:TextToBrush x:Key="TxtToBrush"/>

转换器看起来像这样:

public class TextToBrush : IValueConverter
{
    List<Tuple<string, Brush>> textBrush = new List<Tuple<string, Brush>>()
    {
        new Tuple<string, Brush>("sad", new SolidColorBrush(Colors.Blue))
    };

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value == null)
            return new SolidColorBrush(Colors.Transparent);
        else
        {
            foreach (Tuple<string,Brush> item in textBrush)
                if ((value as string) == item.Item1) return item.Item2 as Brush;
        }
        return new SolidColorBrush(Colors.Transparent);
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

当然,您的媒体资源也BGColor应提高OnPropertyChanged(您的商品类别应增加INotifyPropertyChanged).

速聊1:
嘿,谢谢 我制作转换器,看起来更好.但是现在我尝试将图像设置为背景.如果我将像ImageSource ="{Binding some_url_of_my_asset}"这样的stackpanel属性设置为有可能吗?
速聊2:
StackPanel没有属性ImageSource.您当然可以绑定Image的ImageSource(这样会发现很多答案).如果要将图像设置为背景,则可能对ImageBrush class感兴趣.您可以设置另一个将由getter返回的属性,例如imagebrush.
速聊3:
别客气.请记住,如果答案之一解决了您的问题,请不要忘记接受它.

I have a Listbox containing item binding on my table that consist of 5 columns (idStory, title, created, textStory, and feeling).

<ListBox x:Name="MainListBox" Height="418" SelectionChanged="MainListBox_SelectionChanged">
<ListBox.ItemTemplate>
    <DataTemplate>
        <StackPanel x:Name="listPanel" Orientation="Horizontal" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="0,5,0,0">
            <Rectangle Width="100" Height="100" Fill="#e34d64" />
            <StackPanel Orientation="Vertical" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="15,0,0,0">
                <TextBlock Text="{Binding Title}" FontSize="26" Foreground="Black"/>
                <TextBlock Text="{Binding Created}" FontSize="20" Foreground="Black"/>
                <TextBlock Text="{Binding TextStory}" FontSize="20" Foreground="Black" FontStyle="Italic"/>
            </StackPanel>
            <toolkit:ContextMenuService.ContextMenu>
                <toolkit:ContextMenu>
                    <toolkit:MenuItem x:Name="menuEdit" Header="Edit Story" Click="menuEdit_Click"/>
                    <toolkit:MenuItem x:Name="menuDelete" Header="Delete Story" Click="menuDelete_Click"/>
                </toolkit:ContextMenu>
            </toolkit:ContextMenuService.ContextMenu>
        </StackPanel>
    </DataTemplate>
</ListBox.ItemTemplate>

It works well, but i want to change the background on each item depends on column feeling. For example if we get string "sad" then the listbox item will have blue as a background like this http://imgur.com/n5LoNgj

What should i do for making it real? Any help would be greatly appreciated. Thank you.

Talk1:
I've removed a tag from question's title. Please note that in most cases questions shouldn't include tags in their title.
Talk2:
Oh, i am so sorry. Thank you
Talk3:
Tags are fine in the title if they are integrated in a fluid manner as was done in this question (see the second example in your link).
Solutions1

For your purpose I would propose binding Background of your TextBox (or maybe better whole StackPanel - your choice) to Feeling property with a Converter:

<TextBlock Text="{Binding Title}" FontSize="26" Foreground="Black" Background={Binding Feeling, Converter={StaticResource TxtToBrush}}/>

You will have to add key to your Converter somewhere in the Resources:

xmlns:conv="clr-namespace:Yournamespace"
<conv:TextToBrush x:Key="TxtToBrush"/>

And the Converter can look like this:

public class TextToBrush : IValueConverter
{
    List<Tuple<string, Brush>> textBrush = new List<Tuple<string, Brush>>()
    {
        new Tuple<string, Brush>("sad", new SolidColorBrush(Colors.Blue))
    };

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value == null)
            return new SolidColorBrush(Colors.Transparent);
        else
        {
            foreach (Tuple<string,Brush> item in textBrush)
                if ((value as string) == item.Item1) return item.Item2 as Brush;
        }
        return new SolidColorBrush(Colors.Transparent);
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

And of course your Property BGColor should raise OnPropertyChanged (your item class should impement INotifyPropertyChanged).

Talk1:
Hei, thank you. I make converter and it's look like better. But now i try to set image as my background. Is it possible if i set my stackpanel properties like ImageSource="{Binding some_url_of_my_asset}" ?
Talk2:
StackPanel doesn't have a property ImageSource. You can surely bind ImageSource of your Image (you will find many answer on SO). If you want to set your Image as Background, you may be interested in ImageBrush class. You can make another property which getter would return for example imagebrush.
Talk3:
You are welcome. Remember that if one of the answers solved your problem, don't forget to accept it.
转载于:https://stackoverflow.com/questions/23027666/different-background-on-each-listbox-item

本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文

留言回复
我们只提供高质量资源,素材,源码,坚持 下了就能用 原则,让客户花了钱觉得值
上班时间 : 周一至周五9:00-17:30 期待您的加入