从样式绑定到控件应用样式

时间:2016-08-11 09:26:26

标签: c# wpf xaml data-binding styles

我想创建具有Text-hint的TextBox来描述TextBox值。 为此,我使用TextBlock显示TextBox提示,当TextBox为空时,然后我编码转换器以转换TextBox的Horizo​​ntalContentAlignment(应用该样式的控件)到AlignmentX意味着当目标TextBox例如被留下时则visualBrush显示左对齐TextBlock,但这个pupose没有实现。 我怎么能像吼叫一样解决这个问题:

enter image description here

我的XAML代码:

  <Window.Resources>
    <local:HorizontalAlignToAlignX x:Key="HAligToXAlig"/>
    <Style TargetType="{x:Type TextBox}" xmlns:sys="clr-namespace:System;assembly=mscorlib">
        <Style.Resources>
            <VisualBrush x:Key="CueBannerBrush"
                         Stretch="None"
                         AlignmentX="{Binding HorizontalContentAlignment, Converter={StaticResource HAligToXAlig}}">
                <VisualBrush.Visual>
                    <TextBlock Text="search" Foreground="LightGray"/>
                </VisualBrush.Visual>
            </VisualBrush>
        </Style.Resources>
        <Style.Triggers>
            <Trigger Property="Text" Value="{x:Static sys:String.Empty}">
                <Setter Property="Background" Value="{StaticResource CueBannerBrush}"/>
            </Trigger>
        </Style.Triggers>
    </Style>
</Window.Resources>
<Grid>
    <TextBox x:Name="textBox"
             TextWrapping="Wrap"
             VerticalAlignment="Top"
             Width="342"
             HorizontalContentAlignment="Right" />
</Grid>

我的转换器代码:

    class HorizontalAlignToAlignX : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        AlignmentX Align = AlignmentX.Right;
        switch ((HorizontalAlignment)value)
        {
            case HorizontalAlignment.Left:
                Align = AlignmentX.Left;
                break;
            case HorizontalAlignment.Center:
                Align = AlignmentX.Center;
                break;
            case HorizontalAlignment.Right:
                Align = AlignmentX.Right;
                break;
            case HorizontalAlignment.Stretch:
                Align = AlignmentX.Center;
                break;
            default:
                break;
        }
        return Align;
    }

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

1 个答案:

答案 0 :(得分:1)

我资助我的答案由RelativeSource = {RelativeSource FindAncestor,AncestorType = {x:Type TextBox}},如下所示:

 <Window.Resources>
    <local:HorizontalAlignToAlignX x:Key="HAligToXAlig" />
    <Style TargetType="{x:Type TextBox}"
           xmlns:sys="clr-namespace:System;assembly=mscorlib">
        <Style.Resources>
            <VisualBrush x:Key="CueBannerBrush"
                         Stretch="None"
                         AlignmentX="{Binding ConverterParameter=HorizontalContentAlignment, Converter={StaticResource HAligToXAlig}, Mode=OneWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type TextBox}}, Path=HorizontalContentAlignment}">
                <VisualBrush.Visual>
                    <TextBlock Text="search"
                               Foreground="LightGray"
                               TextAlignment="Right" />
                </VisualBrush.Visual>
            </VisualBrush>
        </Style.Resources>
        <Style.Triggers>
            <Trigger Property="Text"
                     Value="{x:Static sys:String.Empty}">
                <Setter Property="Background"
                        Value="{StaticResource CueBannerBrush}" />
            </Trigger>
        </Style.Triggers>
    </Style>
</Window.Resources>
<Grid>
    <TextBox x:Name="textBox"
             TextWrapping="Wrap"
             VerticalAlignment="Top"
             Width="342"
             HorizontalContentAlignment="Right" />
</Grid>