简体   繁体   English

条件文本格式化XAML WP8

[英]Conditional text formatting XAML WP8

Is it possible to setup some form of conditional formatting of textblock controls in XAML so that the color of the text can be changed depending on the text (eg. Text = "good" then set to green, Text = "bad" then set text to red.) 是否可以在XAML中设置某种形式的文本块控件的条件格式,以便可以根据文本更改文本的颜色(例如,Text =“good”然后设置为green,Text =“bad”然后设置文本红色。)

I have tried some examples but they don't seem to work, presumably because WP8 works differently. 我尝试了一些例子,但它们似乎没有用,大概是因为WP8的工作方式不同。

One simple way is in the view with DataTrigger s like: 一种简单的方法是在DataTrigger的视图中:

Namespaces: 命名空间:

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

Control: 控制:

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

Or you could wire up the condition in code. 或者您可以在代码中连接条件。 Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM