WPF Datatrigger绑定到父绑定(使用Stringformat)

时间:2016-10-07 11:47:11

标签: wpf xaml binding datatrigger

可能是一个菜鸟问题,但如何在datatrigger中找到我的Control的DataContext ...?我无法绑定到文本属性,因为Stringformat这不是绑定值...我不想直接绑定到myText,因为此样式应该应用于ResDic并且Textblocks的Bound属性可能会有所不同(也可以是Stringformat)....

实际上我需要比较实际Textblock的边界值...

    <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 个答案:

答案 0 :(得分:1)

文本块的绑定值将是整个值:例如,“此文本为'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>

答案 1 :(得分:0)

没有转换器方法:

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