Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 每个列表框项目的不同背景_C#_Wpf_Windows Phone 8_Listbox - Fatal编程技术网

C# 每个列表框项目的不同背景

C# 每个列表框项目的不同背景,c#,wpf,windows-phone-8,listbox,C#,Wpf,Windows Phone 8,Listbox,我的表中有一个列表框,其中包含由5列组成的项绑定(idStory、title、created、textStory和feeling) 它工作得很好,但我想改变每个项目的背景取决于列的感觉。例如,如果我们得到字符串“sad”,那么列表框项的背景将是蓝色,如下所示 我该怎么做才能把它变成现实?任何帮助都将不胜感激。谢谢。出于您的目的,我建议使用转换器将文本框(或者更好的是整个StackPanel-您的选择)的背景绑定到Feeling属性: <TextBlock Text="{Binding

我的表中有一个列表框,其中包含由5列组成的项绑定(idStory、title、created、textStory和feeling)


它工作得很好,但我想改变每个项目的背景取决于列的感觉。例如,如果我们得到字符串“sad”,那么列表框项的背景将是蓝色,如下所示


我该怎么做才能把它变成现实?任何帮助都将不胜感激。谢谢。

出于您的目的,我建议使用转换器将文本框(或者更好的是整个StackPanel-您的选择)的背景绑定到Feeling属性:

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

您必须在参考资料中的某个地方向转换器添加密钥:

xmlns:conv="clr-namespace:Yournamespace"
<conv:TextToBrush x:Key="TxtToBrush"/>
xmlns:conv=“clr命名空间:Yournamespace”
转换器可以如下所示:

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();
    }
}
公共类TextToBrush:IValueConverter
{
List textBrush=新列表()
{
新元组(“sad”,新SolidColorBrush(Colors.Blue))
};
公共对象转换(对象值、类型targetType、对象参数、System.Globalization.CultureInfo区域性)
{
如果(值==null)
返回新的SolidColorBrush(颜色:透明);
其他的
{
foreach(textBrush中的元组项)
if((字符串形式的值)=item.Item1)返回item.Item2作为笔刷;
}
返回新的SolidColorBrush(颜色:透明);
}
公共对象转换回(对象值、类型targetType、对象参数、System.Globalization.CultureInfo区域性)
{
抛出新的NotImplementedException();
}
}

当然,您的属性
BGColor
应该提升
OnPropertyChanged
(您的项目类应该是impect
INotifyPropertyChanged
)。

我已经从问题标题中删除了一个标记。请注意,在大多数情况下,都会有问题。哦,我很抱歉。感谢you@Romaz如果标签像这个问题中所做的那样以流畅的方式集成在一起(参见链接中的第二个示例),那么标题中的标签就可以了。嗨,谢谢。我做了转换器,看起来更好。但现在我尝试将图像设置为我的背景。如果我将stackpanel属性设置为ImageSource=“{Binding some_url_of_my_asset}”,是否可能?@SonicMaster stackpanel没有属性ImageSource。您当然可以绑定图像的ImageSource(您将在上面找到许多答案)。如果您想将图像设置为背景,您可能会感兴趣。您可以创建getter将返回的另一个属性,例如imagebrush@SonicMaster不客气。记住,如果其中一个答案解决了你的问题,不要忘记。
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();
    }
}