Style样式中定义控件的数据源如何绑定?

Gin’ 2017-07-13 11:01:48
我给Textbox 想加一个水印效果,然后写了一个Style


Style中有一个Textblock,它是用来显示水印效果的文字的.
我想让这个Textblock的Text数据源绑定到我的定义的一个String 属性上,应该怎么绑定?
...全文
196 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
exception92 2017-07-14
  • 打赏
  • 举报
回复
貌似在Triggers 中找不到,在ControlTemplate 中可以找到它本身的属性,比如 Tag值,你把tip的值放到Tag中,在Template中进行布局绑定。 Text="{TemplateBinding Tag}"
Gin’ 2017-07-14
  • 打赏
  • 举报
回复
引用 1 楼 duanzi_peng 的回复:
可以这样,

<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:s="clr-namespace:System;assembly=mscorlib"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="800" Height="500" WindowStartupLocation="CenterScreen"
Title="" SizeToContent="WidthAndHeight">
<Window.Resources>
<SolidColorBrush x:Key="scb1" Color="Red"></SolidColorBrush>
<s:String x:Key="sd">1231</s:String>
<Style x:Key="myTextBoxStyle1" TargetType="TextBox">
<Style.Triggers>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsFocused" Value="False"></Condition>
<Condition Property="Text" Value=""></Condition>
</MultiTrigger.Conditions>
<Setter Property="Background">
<Setter.Value>
<VisualBrush AlignmentX="Left" AlignmentY="Top">
<VisualBrush.Visual>
<TextBlock FontSize="12" Foreground="Red" Text="{StaticResource sd}"></TextBlock>
</VisualBrush.Visual>
</VisualBrush>
</Setter.Value>
</Setter>
</MultiTrigger>
</Style.Triggers>
</Style>



这个Textblock绑定的TbxTip应该放到哪里才能被它解析到?
exception92 2017-07-13
  • 打赏
  • 举报
回复
可以这样,

<Window
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:s="clr-namespace:System;assembly=mscorlib"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="800" Height="500" WindowStartupLocation="CenterScreen"
  Title="" SizeToContent="WidthAndHeight">
  <Window.Resources>
    <SolidColorBrush x:Key="scb1" Color="Red"></SolidColorBrush>
    <s:String x:Key="sd">1231</s:String>
    <Style x:Key="myTextBoxStyle1" TargetType="TextBox">
       <Style.Triggers>
          <MultiTrigger>
             <MultiTrigger.Conditions>
               <Condition Property="IsFocused" Value="False"></Condition>
               <Condition Property="Text" Value=""></Condition>
             </MultiTrigger.Conditions>
             <Setter Property="Background">
               <Setter.Value>
                    <VisualBrush AlignmentX="Left" AlignmentY="Top">
                       <VisualBrush.Visual>
                          <TextBlock FontSize="12" Foreground="Red" Text="{StaticResource sd}"></TextBlock>
                       </VisualBrush.Visual>
                    </VisualBrush>
               </Setter.Value>
             </Setter>
          </MultiTrigger>
       </Style.Triggers>
    </Style>

8,735

社区成员

发帖
与我相关
我的任务
社区描述
WPF/Silverlight相关讨论
社区管理员
  • WPF/Silverlight社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧