每个列表框项目的背景不同
我有一个列表框,其中包含表上的项目绑定,该列表由5列(idStory,标题,创建的,textStory和feel)组成.
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>
效果很好,但是我想根据列的感觉更改每个项目的背景.例如,如果我们得到字符串"sad",则列表框项将具有蓝色作为背景,例如 http://imgur.com/n5LoNgj
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.
为了您的目的,我建议将您的 TextBox 的背景(或者更好的整个 StackPanel - 您的选择)绑定到带有转换器的 Feeling 属性:
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}}/>
您将必须在资源"中的某个位置向您的Converter添加密钥:
You will have to add key to your Converter somewhere in the Resources:
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
).
And of course your Property BGColor
should raise OnPropertyChanged
(your item class should impement INotifyPropertyChanged
).