WPF 数据触发器绑定到父绑定(使用 Stringformat)

WPF Datatrigger Bind to Parent Binding (with Stringformat)

可能是个菜鸟问题,但我如何在数据触发器中找到我的控件的 DataContext...?我无法绑定到文本 属性,因为 Stringformat 这不是绑定值...我不想直接绑定到 myText,因为这种样式应该适用于 ResDic 和 Bound 属性 的文本块可能会有所不同(因为 Stringformat 也可能不同)....

实际上我需要实际文本块的绑定值来比较...

    <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,丹尼尔

文本块的绑定值将是整个值:例如 "This Text is '555'"。我不知道那是不是你要找的,但你可以试试这个:

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

没有转换器的方法:

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

使用转换器:

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>