在VisualBrush中绑定文本框的宽度

5
我是一名有用的助手,可以为您翻译文本。

我有一个小问题,似乎无法解决。 我有一个文本框,我正在添加“搜索提示”

enter image description here

我正在使用这个XAML片段

<TextBox x:Name="txtboxSearch" Height="22" Margin="3,35,111,0" TextWrapping="Wrap" VerticalAlignment="Top" BorderThickness="1" MaxLines="1" MaxLength="256" Grid.Column="2" BorderBrush="#FF828790">
        <TextBox.Style>
            <Style TargetType="TextBox" xmlns:sys="clr-namespace:System;assembly=mscorlib">
                <Style.Resources>
                    <VisualBrush x:Key="CueBannerBrush" AlignmentX="Left" AlignmentY="Center" Stretch="None">
                        <VisualBrush.Visual>
                            <TextBox Text="Search" Foreground="LightGray" FontStyle="Italic" />
                        </VisualBrush.Visual>
                    </VisualBrush>
                </Style.Resources>
                <Style.Triggers>
                    <Trigger Property="Text" Value="{x:Static sys:String.Empty}">
                        <Setter Property="Background" Value="{StaticResource CueBannerBrush}" />
                    </Trigger>
                    <Trigger Property="Text" Value="{x:Null}">
                        <Setter Property="Background" Value="{StaticResource CueBannerBrush}" />
                    </Trigger>
                    <Trigger Property="IsKeyboardFocused" Value="True">
                        <Setter Property="Background" Value="White" />
                    </Trigger>
                </Style.Triggers>
            </Style>
        </TextBox.Style>
    </TextBox>

因为窗口的背景不是白色,所以我得到了如图所示的结果。我尝试过多种不同的方式来绑定宽度,但似乎都不起作用,您能否提供建议?
我希望它看起来像这样 enter image description here 谢谢!

你究竟想要实现什么? - Olaru Mircea
VisualBrush 中的文本框宽度与 "textboxsearch" 的宽度相同。 - Dude
1个回答

2

实现这个目标的方法是将 CueBannerBrush(您的 VisualBrush)中 TextBox 的宽度绑定到主 TextBox 父级(txtboxSearch)。

但是,只有在将 CueBannerBrush 定义放置在控件或窗口资源中而不是 TextBox.Style 资源中时才起作用:

<Window.Resources>
    <VisualBrush x:Key="CueBannerBrush" TileMode="Tile" Stretch="None"  AlignmentX="Left" AlignmentY="Center" >
        <VisualBrush.Visual>
            <TextBox 
                    Width="{Binding ElementName=txtboxSearch, Path=ActualWidth}"
                    HorizontalAlignment="Stretch" x:Name="txtboxWatermark" Text="Search" Foreground="LightGray" FontStyle="Italic"/>
        </VisualBrush.Visual>
    </VisualBrush>
</Window.Resources>

然后你的主要XAML就是相同的,但没有VisualBrush定义。

<Grid Background="Tomato">
    <TextBox x:Name="txtboxSearch" Height="22" Margin="3,35,111,0" TextWrapping="Wrap" VerticalAlignment="Top" BorderThickness="1" 
             MaxLines="1" MaxLength="256" Grid.Column="2" BorderBrush="#FF828790">
        <TextBox.Style>
            <Style TargetType="TextBox">
                <Style.Triggers>
                    <Trigger Property="Text" Value="{x:Static sys:String.Empty}">
                        <Setter Property="Background" Value="{StaticResource CueBannerBrush}" />                            
                    </Trigger>
                    <Trigger Property="Text" Value="{x:Null}">
                        <Setter Property="Background" Value="{StaticResource CueBannerBrush}" />
                    </Trigger>
                    <Trigger Property="IsKeyboardFocused" Value="True">
                        <Setter Property="Background" Value="White" />
                    </Trigger>
                </Style.Triggers>
            </Style>
        </TextBox.Style>
    </TextBox>
</Grid>

现在你会看到最初的这个样子: : 当你点击它时: :

太好了!谢谢。 - Dude

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接