locked
Bound Textblock String Conditional Foreground Color (DataTrigger) ? RRS feed

  • Question

  • Hopefully someone with experience in these can share some insight or a better example than I found to accomplish this.

    Say I have a bound textblock that will return say True, False, Maybe. I'd like to set a DataTrigger to change the foreground color based on the string returned. So for example True = Green, False = Red, etc. I tried a couple routes but didn't get any success, looking forward to working with these as they could be very helpful in other projects.

    So basically what I would like to accomplish, would be to do this with setters in xaml I imagine? Not sure if changepropertyaction is required?:

    <TextBlock x:Name="VerifiedText" Text="{Binding 
        Path=Verification.Please, Mode=TwoWay}"/>

    if Verifiedtext=True

    foreground=green

    if VerifiedText=False

    foreground=red

    you get the idea... I've tried a couple ways patching together from other articles just need a better silverlight & preferably xaml example. Thanks in advance for any shared experience. :)


    Please mark answers as helpful when used, and answered when completed.


    Tuesday, April 3, 2012 9:55 PM

Answers

  • Did you try the DataTrigger?

    <TextBlock x:Name="textBlock" HorizontalAlignment="Left" TextWrapping="Wrap" Text="true" VerticalAlignment="Bottom" Margin="207,0,0,220">
    			<i:Interaction.Triggers>
    				<ei:DataTrigger Value="false" Binding="{Binding Text, ElementName=textBlock}">
    					<ei:ChangePropertyAction PropertyName="Foreground">
    						<ei:ChangePropertyAction.Value>
    							<SolidColorBrush Color="Red"/>
    						</ei:ChangePropertyAction.Value>
    					</ei:ChangePropertyAction>
    				</ei:DataTrigger>
    				<ei:DataTrigger Value="true" Binding="{Binding Text, ElementName=textBlock}">
    					<ei:ChangePropertyAction PropertyName="Foreground">
    						<ei:ChangePropertyAction.Value>
    							<SolidColorBrush Color="Green"/>
    						</ei:ChangePropertyAction.Value>
    					</ei:ChangePropertyAction>
    				</ei:DataTrigger>
    			</i:Interaction.Triggers>
    		</TextBlock>

    Or is that not what you are looking for?

    ~Christine

    Tuesday, April 3, 2012 10:47 PM

All replies

  • Did you try the DataTrigger?

    <TextBlock x:Name="textBlock" HorizontalAlignment="Left" TextWrapping="Wrap" Text="true" VerticalAlignment="Bottom" Margin="207,0,0,220">
    			<i:Interaction.Triggers>
    				<ei:DataTrigger Value="false" Binding="{Binding Text, ElementName=textBlock}">
    					<ei:ChangePropertyAction PropertyName="Foreground">
    						<ei:ChangePropertyAction.Value>
    							<SolidColorBrush Color="Red"/>
    						</ei:ChangePropertyAction.Value>
    					</ei:ChangePropertyAction>
    				</ei:DataTrigger>
    				<ei:DataTrigger Value="true" Binding="{Binding Text, ElementName=textBlock}">
    					<ei:ChangePropertyAction PropertyName="Foreground">
    						<ei:ChangePropertyAction.Value>
    							<SolidColorBrush Color="Green"/>
    						</ei:ChangePropertyAction.Value>
    					</ei:ChangePropertyAction>
    				</ei:DataTrigger>
    			</i:Interaction.Triggers>
    		</TextBlock>

    Or is that not what you are looking for?

    ~Christine

    Tuesday, April 3, 2012 10:47 PM
  • Holy crap Christine, way to make me have a "duh..." moment! I forgot to bind to the #$*&@ Text value instead of...ah nevermind! Cheers! Always good to have another pair of eyes around. Thanks! :)

    Please mark answers as helpful when used, and answered when completed.

    Tuesday, April 3, 2012 11:08 PM
  • I thought your question was a little unusual for you.  Your a champ at triggers.

    ~Christine

    Tuesday, April 3, 2012 11:14 PM
  • Nah haven't had an opportunity to use a lot of datatriggers, just interaction mainly. One of these days I need to send you and Chuck fruit baskets or something for as many times as you catch my follies haha

    Please mark answers as helpful when used, and answered when completed.

    Tuesday, April 3, 2012 11:23 PM
  • Ooh, fruit baskets! :)
    Wednesday, April 4, 2012 1:12 PM
    Moderator
  • Ya well I quit offering beer, defeats the purpose of a gift when you end up taking half of it back :)

    Please mark answers as helpful when used, and answered when completed.

    Wednesday, April 4, 2012 2:18 PM