Conditional text formatting XAML WP8

混江龙づ霸主 提交于 2019-12-10 20:00:11

问题


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

I have tried some examples but they don't seem to work, presumably because WP8 works differently.


回答1:


One simple way is in the view with DataTriggers like:

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.



来源:https://stackoverflow.com/questions/15905195/conditional-text-formatting-xaml-wp8

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!