.net 如何根据WPF中的bool属性设置背景颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/345198/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 10:45:56  来源:igfitidea点击:

How to set background color based on bool property in WPF

.netwpfdata-bindingc#-3.0styles

提问by Russ

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 中的列表视图内数据绑定的 GridViewColumn 设置背景颜色。我不知道如何问这个对 WPF 相当陌生的问题,否则我不会打扰你们所有人。

I want to change the background color of the whole row, based on a bool flag in my databound object.

我想根据数据绑定对象中的 bool 标志更改整行的背景颜色。

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.

在这种情况下,我有一个“CaseDetail”对象,当有内部注释“IsInternalNote”时,我希望改变行的颜色。

How can I pull this off in WPF?

我怎样才能在 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>

回答by Thomas

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 :)

可能有其他选择,但是一旦我开始工作,我就不再寻找了:)

回答by Bryan Anderson

I haven't tested this yet so it might need some tweaks but you're going to want to trigger off the value to set your background color.

我还没有测试过这个,所以它可能需要一些调整,但你会想要触发值来设置你的背景颜色。

<DataTemplate.Triggers>
    <Trigger Property="IsInternalNote" Value="True">
        <Setter Property="Background" Value="Red" />
    </Trigger>
</DataTemplate.Triggers>