且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如果行为空,如何为列表视图的行设置背景或边框画笔颜色?

更新时间:2023-01-26 23:31:15

Hi Ankit,

Hi Ankit,

在你的标记中,你有

   <ListView.ItemContainerStyle>
        <Style TargetType="ListViewItem">
                <Setter Property="HorizontalContentAlignment" Value="Stretch" />
        </Style>
   </ListView.ItemContainerStyle>

每一行都在listviewitem中,您需要为该样式添加内容。

Each row is in a listviewitem and you will want to add something to that style.

我建议您下载snoop并查看从标记中获得的内容。如果你还没有使用它,它对于任何wpf开发人员来说都是必须的。

I suggest you download snoop and take a look at what you get from your markup. If you don't use it already, it's pretty much a must have for any wpf developer.

listview项目有一个你可以绑定或设置的背景属性。我建议这里数据触发器可能是***的。

A listview item has a background property you could bind or set. I suggest here a datatrigger is probably best.

没有自动任何字段是零长度或空标记,你可以放在那里,所以你需要一些东西来做那个逻辑。 你可以将它放在你的viewmodel中并公开一个公共bool来驱动你的数据触发器,或者你可以编写一个转换器。
为了做到这一点,它可能必须是一个多转换器和多重绑定。如果你肯定知道空意味着null那么多数据触发器可以做到这一点。

There is no automatic any field is zero length or null markup you can put in there so you need something to do that logic.  You could put that in your viewmodel and expose a public bool to drive your datatrigger or you could write a converter. To do that neatly it would probably have to be a multiconverter and multibinding. If you knew for sure that empty means null then a multidatatrigger could do this.

无论如何,具有viewmodel属性的数据触发器看起来像:

Anyhow, a datatrigger with a viewmodel property would look something like:

      <Style.Triggers>
        <DataTrigger Binding="{Binding AnyEmpty}"
                      Value="true">
          <Setter Property="Background"
                  Value="Red" />
        </DataTrigger>
      </Style.Triggers>


显然,AnyEmpty需要遍历字符串并根据您的逻辑返回true / false。

Obviously, AnyEmpty would need to iterate through the strings and return true/false based on your logic.