问题描述
我的表上有一个包含项目绑定的列表框,该列表框由 5 列(idStory、标题、创建、textStory 和感觉)组成.
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}}/>
您必须在资源中的某处为转换器添加密钥:
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
).
这篇关于每个列表框项目的不同背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!