如何在WPF中基于bool属性设置背景色 [英] How to set background color based on bool property in WPF
问题描述
我想为GridViewColumn设置背景色,该GridViewColumn在WPF中的列表视图内是数据绑定的。我不确定如何问这个问题对WPF来说还很陌生,否则我不会打扰你们所有人。
I want to set the backgroun color for a GridViewColumn that is databound inside of a listview in WPF. I'm not sure how to ask this question being fairly new to WPF, otherwise I wouldn't have bothered all of you.
我想更改WPF的背景颜色整个行基于我的数据绑定对象中的布尔标志。
I want to change the background color of the whole row, based on a bool flag in my databound object.
在这种情况下,我有一个 CaseDetail对象,当有内部注释时 IsInternalNote,我想更改行的颜色。
In this case, I have, well, a "CaseDetail" object, that when there are internal notes "IsInternalNote" I want the color of the row to change.
如何在WPF中实现此功能?
How can I pull this off in WPF?
我现在所拥有的(非常简单),不会改变颜色。
What I have now, ( very simple ), which does NOT change the color.
<ListView ItemsSource="{Binding}" IsSynchronizedWithCurrentItem="True" >
<ListView.View>
<GridView>
<GridViewColumn DisplayMemberBinding="{Binding Date, StringFormat=MMM dd\, yyyy h:mm tt}" Header="Date" Width="Auto" />
<GridViewColumn DisplayMemberBinding="{Binding SubmittedBy}" Header="Submitted By" Width="Auto" />
<GridViewColumn Width="Auto" Header="Description" x:Name="colDesc">
<GridViewColumn.CellTemplate>
<DataTemplate>
<ScrollViewer MaxHeight="80" Width="300">
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding Description}" TextWrapping="Wrap" />
<TextBlock Text="{Binding File.FileName}" TextWrapping="Wrap" />
</StackPanel>
</ScrollViewer>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
推荐答案
我在尝试执行此操作时遇到了一些麻烦,这样结束
I ran into a few troubles trying to do this, ended up like so
<ListBox ...>
<ListBox.ItemTemplate>
<DataTemplate>
<Border x:Name="BGBorder">
<!-- -->
</Border>
<DataTemplate.Triggers>
<DataTrigger
Binding="{Binding Path=DataContext.IsAborted, RelativeSource={RelativeSource TemplatedParent}}"
Value="True">
<Setter Property="Background" TargetName="BGBorder" Value="Red">
</Setter>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
也许还有其他选择,但是一旦我工作了,我就会停止寻找:)
There may be alternatives, but once I had it working, I stopped looking :)
这篇关于如何在WPF中基于bool属性设置背景色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!