`TextBlock`的`TextWrapping`属性没有在条件上设置为`Wrap`

时间:2022-08-16 19:35:25

The TextWrapping property was setting to Wrap in this code

TextWrapping属性在此代码中设置为Wrap

<ListView Name="answerListView" ItemsSource="{Binding Path=answers}">
  <ListView.ItemTemplate>
    <DataTemplate>
      <StackPanel>
        <Expander Cursor="Hand">
          <Expander.Header>
            <TextBlock Text="{Binding Path=Body_Markdown}" TextWrapping="Wrap"/>
          </Expander.Header>
        </Expander>
      </StackPanel>
    </DataTemplate>
  </ListView.ItemTemplate>
</ListView>

But, now I have added conditional formatting to the TextBlock, i.e., if answer is accepted then show it in green colour. So the code I have used is this:

但是,现在我已经为TextBlock添加了条件格式,即,如果接受了答案,则将其显示为绿色。所以我使用的代码是这样的:

<ListView Name="answerListView" ItemsSource="{Binding Path=answers}">
  <ListView.ItemTemplate>
    <DataTemplate>
      <StackPanel>
        <Expander Cursor="Hand">
          <Expander.Header>
            <TextBlock Text="{Binding Path=Body_Markdown}">
              <TextBlock.Style>
                <Style TargetType="{x:Type TextBlock}">
                  <Style.Triggers>
                    <DataTrigger Binding="{Binding Path=is_accepted}" Value="true">
                      <Setter Property="Foreground" Value="Green"/>
                      <Setter Property="TextWrapping" Value="Wrap"/>
                    </DataTrigger>
                  </Style.Triggers>
                </Style>
              </TextBlock.Style>
            </TextBlock>
          </Expander.Header>
        </Expander>
      </StackPanel>
    </DataTemplate>
  </ListView.ItemTemplate>
</ListView>

Here, the second line of setting, i.e., Foreground property is not working. Even if I have the same line after <Style TargetType> or TextWrapping="Wrap" in TextBlock, then also it is not working.

这里,第二行设置,即Foreground属性不起作用。即使我在TextBlock中的

3 个解决方案

#1


0  

Specify the type too:

同时指定类型:

<Setter Property="TextBlock.Foreground" Value="Green"/>
<Setter Property="TextBlock.TextWrapping" Value="Wrap"/>

#2


0  

I guess you are binding a private field, instead of the public property:

我猜你绑定了一个私有字段,而不是公共属性:

{Binding Path=is_accepted} should be replaced by your property {Binding Path=Is_accepted}

This answer assumes that you are using the usual naming which makes fields start by lower case and Properties by Upper case.

这个答案假定你使用的是通常的命名,它使字段以小写字母开头,属性以大写字母开头。

#3


0  

ScrollViewer.HorizontalScrollBarVisibility="Disabled" must been added to ListView. Then this problem will be solved.

必须将ScrollViewer.Horizo​​ntalScrollBarVisibility =“已禁用”添加到ListView。那么这个问题就解决了。

#1


0  

Specify the type too:

同时指定类型:

<Setter Property="TextBlock.Foreground" Value="Green"/>
<Setter Property="TextBlock.TextWrapping" Value="Wrap"/>

#2


0  

I guess you are binding a private field, instead of the public property:

我猜你绑定了一个私有字段,而不是公共属性:

{Binding Path=is_accepted} should be replaced by your property {Binding Path=Is_accepted}

This answer assumes that you are using the usual naming which makes fields start by lower case and Properties by Upper case.

这个答案假定你使用的是通常的命名,它使字段以小写字母开头,属性以大写字母开头。

#3


0  

ScrollViewer.HorizontalScrollBarVisibility="Disabled" must been added to ListView. Then this problem will be solved.

必须将ScrollViewer.Horizo​​ntalScrollBarVisibility =“已禁用”添加到ListView。那么这个问题就解决了。