【问题标题】:`TextBlock`'s `TextWrapping` property not getting set to `Wrap` on conditions`TextBlock` 的 `TextWrapping` 属性未在条件下设置为 `W​​rap`
【发布时间】:2016-03-09 11:18:41
【问题描述】:

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>

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

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

这里,第二行设置,即Foreground属性不起作用。即使我在&lt;Style TargetType&gt;TextWrapping="Wrap"TextBlock 之后有相同的行,那么它也不起作用。

【问题讨论】:

  • 你能给我们看看 ViewModel 吗?

标签: wpf xaml conditional textblock dynamic-binding


【解决方案1】:

ScrollViewer.HorizontalScrollBarVisibility="Disabled" 必须添加到 ListView。那么这个问题就迎刃而解了。

【讨论】:

    【解决方案2】:

    也指定类型:

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

    【讨论】:

    • @amitjha 能否请您出示一下您的ModelViewModel
    • 我要粘贴代码吗?这将很难理解。
    • @amitjha 发布必要的代码 - Model,以及来自 ViewModel 的属性。
    • @amitjha 请阅读本文并在此处发布您的代码。如果您在此处发布代码,您将从其他人那里获得更多帮助。在主题中查看比在其他一些资源中查看更方便。
    【解决方案3】:

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

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

    此答案假设您使用的是通常的命名方式,即字段以小写字母开头,属性以大写字母开头。

    【讨论】:

    • 不,is_accepted 值是公共财产。我已按原样使用这些属性,因为它们以 JSON 形式返回。
    • 我要粘贴代码吗?这将很难理解。
    • 抱歉,我看不出您的视图和视图模型之间有任何关系。您在视图中绑定的所有属性都不存在于您的视图模型中...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-18
    • 1970-01-01
    • 2016-08-27
    • 2011-05-05
    相关资源
    最近更新 更多