首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在ListBox ItemsSource对象的属性上使用绑定来设置ListBoxItem的样式?

如何在ListBox ItemsSource对象的属性上使用绑定来设置ListBoxItem的样式?
EN

Stack Overflow用户
提问于 2009-08-20 10:05:00
回答 2查看 39.7K关注 0票数 16

我有一个绑定到LogMessages的ObservableCollection的ListBox。

代码语言:javascript
复制
public ObservableCollection<LogMessage> LogMessages { get; set; }
public LogMessageData()
{
    this.LogMessages = new ObservableCollection<LogMessage>();
}

每条消息有两个参数:

代码语言:javascript
复制
public class LogMessage
{
    public string Msg { get; set; }
    public int Severity { get; set; }
    //code cut...
}

ListBox将填充这些项,我需要根据项的Severity参数更改LogMessage (change a background of ListBoxItem)列表的颜色代码。

下面是我在用户控件的XAML中显示日志的内容:

代码语言:javascript
复制
    <UserControl.Resources>
    <AlternationConverter x:Key="BackgroundSeverityConverter">
        <SolidColorBrush>Green</SolidColorBrush>
        <SolidColorBrush>Yellow</SolidColorBrush>
        <SolidColorBrush>Red</SolidColorBrush>
    </AlternationConverter>
    <Style x:Key="BindingAlternation" TargetType="{x:Type ListBoxItem}">
        <Setter Property="Background" 
                Value="{Binding RelativeSource={RelativeSource TemplatedParent}, 
                Path=Severity, 
                Converter={StaticResource BackgroundSeverityConverter}}"/>
    </Style>
    <DataTemplate x:Key="LogDataTemplate">
        <TextBlock x:Name="logItemTextBlock" Width="Auto" Height="Auto" 
        Text="{Binding Msg}"/>
    </DataTemplate>
</UserControl.Resources>

和一个实际的ListBox:

代码语言:javascript
复制
<ListBox IsSynchronizedWithCurrentItem="True" 
    ItemTemplate="{DynamicResource LogDataTemplate}" 
    ItemsSource="{Binding LogFacility.LogMessages}" 
    x:Name="logListBox" Grid.Row="1" 
    ItemContainerStyle="{StaticResource BindingAlternation}" />

之所以使用AlternationConverter,是因为message的Severity参数的类型为Int (0..3),我们可以使用该参数轻松地在样式之间进行切换。

这个概念很清楚,但到目前为止,它对我还不起作用。ListBoxItem的背景色没有改变。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2009-08-20 10:38:48

使用ItemContainerStyle

代码语言:javascript
复制
<ListBox ItemsSource="{Binding LogMessages}">
    <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
            <Setter Property="Background" Value="{Binding Severity, Converter={StaticResource YourBackgroundConverter}}"/>
        </Style>
    </ListBox.ItemContainerStyle>
</ListBox>
票数 29
EN

Stack Overflow用户

发布于 2009-08-20 10:46:38

就像Bojan评论的那样,RelativeSource不应该出现在那里。在绑定到数据对象时使用{Binding Path=Severity,Converter={StaticResource BackgroundSeverityConverter}}。RelativeSource.TemplatedParent用于绑定到ListBoxItem。

另外,我有一点恼火,你可以考虑使用触发器,例如:

代码语言:javascript
复制
<Style x:Key="BindingAlternation" TargetType="{x:Type ListBoxItem}">
    <Style.Triggers>
        <DataTrigger Binding="{Binding Severity}" Value="1">
            <Setter Property="Background" Value="Green"/>
        </DataTrigger>
        <DataTrigger Binding="{Binding Severity}" Value="2">
            <Setter Property="Background" Value="Yellow"/>
        </DataTrigger>
        <!-- etc.. -->
    </Style.Triggers>
<Style x:Key="BindingAlternation" TargetType="{x:Type ListBoxItem}">

但这只是一个个人preference....what,如果你修复了绑定,它应该可以正常工作。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1305157

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档