【问题标题】:ListView Style Trigger to change string formatListView 样式触发器更改字符串格式
【发布时间】:2018-10-17 19:14:42
【问题描述】:

我正在尝试使用样式触发器向 listview.item 添加星号。但是,下面的代码似乎忽略了 ContentStringFormat。项目的背景正确设置为红色。有人能发现为什么 ContentStringFormat 的 setter 属性不起作用吗?

<ListView.Resources>
      <Style TargetType="{x:Type ListViewItem}">
          <Style.Triggers>
              <DataTrigger Binding="{Binding Path=IsTrue}" Value="true">
                  <Setter Property="Background" Value="Red"/>
                   <Setter Property="ContentStringFormat" Value="{}{0} *}"/>
                    </DataTrigger>
           </Style.Triggers>
     </Style>
</ListView.Resources>

【问题讨论】:

  • 尾随花括号是故意的吗? * 后面的那个,因为它们不匹配。

标签: wpf xaml listview


【解决方案1】:

刚刚测试了您的代码,可以确认ContentFormatString 末尾的花括号导致了问题。删除它并像这样使用它:

<ListView.Resources>
    <Style TargetType="{x:Type ListViewItem}">
        <Style.Triggers>
            <DataTrigger Binding="{Binding Path=IsTrue}" Value="true">
                <Setter Property="Background" Value="Red"/>
                <Setter Property="ContentStringFormat" Value="{}{0} *"/><!-- Here is the change-->
            </DataTrigger>
        </Style.Triggers>
    </Style>
</ListView.Resources>

【讨论】:

  • 嗯,我希望这会解决它,但它仍然无法正常工作
  • 您必须与我们分享更多 xaml。我测试了您提供的代码,ListView 生成带有* 的项目,最后带有红色背景(提供了修复程序)。我在MainWindow.xaml 的网格内使用了我的。
  • 我创建了一个gist 给你看看。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-25
  • 1970-01-01
  • 2016-09-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多