【问题标题】:Conditional text formatting XAML WP8条件文本格式 XAML WP8
【发布时间】:2013-04-01 01:48:22
【问题描述】:

是否可以在 XAML 中设置某种形式的文本块控件的条件格式,以便可以根据文本更改文本的颜色(例如,Text = "good" 然后设置为绿色,Text = "bad"然后将文本设置为红色。)

我尝试了一些示例,但它们似乎不起作用,大概是因为 WP8 的工作方式不同。

【问题讨论】:

  • 你能发布一些你已经尝试过的例子吗?它有助于了解哪些不起作用

标签: xaml windows-phone-8 formatting conditional


【解决方案1】:

一种简单的方法是在视图中使用DataTriggers,例如:

命名空间:

xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"

控制:

<TextBlock x:Name="TheText" Text="{Binding Blah}"/>
    <i:Interaction.Triggers>
      <ei:DataTrigger Value="Red"
                      Binding="{Binding Text, ElementName=TheText}">
          <ei:ChangePropertyAction PropertyName="Foreground"
                                   Value="Red" />
      </ei:DataTrigger>
      <ei:DataTrigger Value="Blue"
                      Binding="{Binding Text, ElementName=TheText}">
          <ei:ChangePropertyAction PropertyName="Foreground"
                                   Value="Blue" />
       </ei:DataTrigger>
    </i:Interaction.Triggers>
  </TextBlock>

或者您可以在代码中连接条件。希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-04-20
    • 1970-01-01
    • 2021-08-29
    • 2016-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多