Search code examples
wpfxamlconditional-statementstextblockdynamic-binding

`TextBlock`'s `TextWrapping` property not getting set to `Wrap` on conditions


The TextWrapping property was setting to Wrap in this code

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

<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.


Solution

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