3
我正在实施搜索文本框;你能帮我绑定到TextBox.Tag吗?如何将wpf中的样式绑定到标记?
风格
<Style x:Key="SearchTextBox" TargetType="{x:Type TextBox}">
<Style.Resources>
<VisualBrush x:Key="CueBannerBrush" AlignmentX="Left" AlignmentY="Center" Stretch="None">
<VisualBrush.Visual>
<Label Content="{Binding RelativeSource={RelativeSource Mode=Self}, Path=Tag}" Foreground="{StaticResource SearchTextBox.Foreground}" FontSize="{StaticResource SearchTextBox.FontSize}"/>
</VisualBrush.Visual>
</VisualBrush>
</Style.Resources>
<Setter Property="FontSize" Value="{StaticResource SearchTextBox.FontSize}" />
<Setter Property="Foreground" Value="{StaticResource SearchTextBox.TextForeground}" />
<Setter Property="MinWidth" Value="200" />
<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="{StaticResource SearchTextBox}" Tag="Search templates" />
我怎样才能绑定工作?
谢谢你,比尔。我发现你的方法是创建模板... –
不客气。正如塞缪尔杰克在我提供的链接中所说的,如果您在风格中使用资源,那么它们只会被实例化一次。所以,如果你有多个文本框,那么他们总是有相同的'提示'文本。 –
是的,我注意到了!我的第一个实现是通过附加属性,它有默认文本!))))再次感谢你。 –