0
votes

may be a noob question, but how do I find the DataContext of my Control in datatrigger...? I can't bind to the text Property, since the Stringformat this is not the bound value... I don't want to bind directly to myText, since this style should apply in a ResDic and the Bound Property of the Textblocks may vary (as Stringformat may too)....

Actually I need the bound value of the actual Textblock to compare...

    <TextBlock Text="{Binding myText, StringFormat='This Text is: {0}'}">
        <TextBlock.Style>
            <Style TargetType="TextBlock">
                <Style.Triggers>
                    <DataTrigger Binding="????"
                                 Value="555">
                        <Setter Property="Foreground"
                                Value="Red" />
                    </DataTrigger>

                </Style.Triggers>
            </Style>
        </TextBlock.Style>
    </TextBlock>

BR, Daniel

2

2 Answers

0
votes

Without converter approach :

<TextBlock>
   <TextBlock.Inlines>
       <Run Text="This Test is : "/>
       <Run x:Name="RunMyText" Text="{Binding myText}"/>                    
   </TextBlock.Inlines>                    
   <TextBlock.Style>
       <Style TargetType="TextBlock">
           <Setter Property="Foreground" Value="Blue" />
           <Style.Triggers>
               <DataTrigger Binding="{Binding Text, ElementName=RunMyText}"  Value="555">
                  <Setter Property="Foreground" Value="Red" />
               </DataTrigger>
            </Style.Triggers>
        </Style>
    </TextBlock.Style>
</TextBlock>

Using converter :

public class MyTextConverter : IValueConverter
{

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return Int32.Parse(value.ToString());
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

XAML :

<TextBlock Text="{Binding myText, StringFormat= 'This Text is: {0}'}">
    <TextBlock.Resources>
       <local:MyTextConverter x:Key="MyTextCnvKey"/>
    </TextBlock.Resources>
    <TextBlock.Style>
        <Style TargetType="TextBlock">
            <Style.Triggers>
                <DataTrigger Binding="{Binding myText, Converter={StaticResource MyTextCnvKey}}"
                        Value="555">
                    <Setter Property="Foreground"
                    Value="Red" />
                </DataTrigger>

            </Style.Triggers>
        </Style>
   </TextBlock.Style>
</TextBlock>
1
votes

The bound value of the textblock would be the whole value: "This Text is '555'", for example. I don't know if that's what you're looking for, but you can try this:

      <TextBlock Text="{Binding myText, StringFormat='This Text is: {0}'}" >
        <TextBlock.Style>
            <Style TargetType="TextBlock">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=Text}"
                             Value="This Text is: 555">
                        <Setter Property="Foreground"
                            Value="Red" />
                    </DataTrigger>

                </Style.Triggers>
            </Style>
        </TextBlock.Style>
    </TextBlock>