【问题标题】:Bind to controls property in corresponding style绑定到相应样式的控件属性
【发布时间】:2021-01-11 23:56:22
【问题描述】:

我想为文本框定义一个样式,并在我想绑定到相应样式所适用的属性的样式内。像这样的:

<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
   <Style.Triggers>
      <Trigger Property="IsEnabled" Value="True">
         <Setter Property="ToolTip">
            <Setter.Value>
               <TextBlock Text="{Binding ????Bind to the textbox TEXT property????}">
               </TextBlock>
            </Setter.Value>
         </Setter>
      </Trigger>
   </Style.Triggers>
</Style>

这可能吗?

这里是完整的窗口:

<Window x:Class="StyleBinding.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:StyleBinding"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Window.Resources>
        <Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
            <Style.Triggers>
                <Trigger Property="IsEnabled" Value="True">
                    <Setter Property="ToolTip">
                        <Setter.Value>
                            <TextBlock Text="{Binding Text, RelativeSource={RelativeSource Self}}"/>
                        </Setter.Value>
                    </Setter>
                </Trigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>
    <Grid>
        <TextBox HorizontalAlignment="Left" Height="34" Margin="235,140,0,0" TextWrapping="Wrap" IsEnabled="True"
                 Text="Just a simple text" VerticalAlignment="Top" Width="284">
        </TextBox>
    </Grid>
</Window>

【问题讨论】:

    标签: c# wpf xaml data-binding binding


    【解决方案1】:

    您只需使用RelativeSource 绑定即可访问TextBoxText 属性。

    <Setter Property="ToolTip" Value="{Binding Text, RelativeSource={RelativeSource Self}}"/>
    

    如果您按照自己的风格构建自定义工具提示模板,您可以这样做。

    <Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
       <Setter Property="ToolTip">
          <Setter.Value>
             <ToolTip>
                <TextBlock Text="{Binding PlacementTarget.Text, RelativeSource={RelativeSource AncestorType={x:Type ToolTip}}}"/>
             </ToolTip>
          </Setter.Value>
       </Setter>
    </Style>
    

    ToolTip 中的PlacementTarget 在这里是TextBox

    【讨论】:

      【解决方案2】:

      这是一个工作示例:

      <Style BasedOn="{StaticResource {x:Type TextBox}}" TargetType="{x:Type TextBox}">
          <Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Path=Text}" />
      </Style>
      

      关键是使用SelfRelativeSource 绑定。

      无需在IsEnabled 上设置触发器,因为默认情况下ToolTip 只会显示在启用的控件上。

      【讨论】:

      • 谢谢,我想我的问题是我有工具提示作为文本块: 所以它使用的是文本块而不是文本框本身。在这种情况下如何绑定到父文本框?
      猜你喜欢
      • 1970-01-01
      • 2021-04-20
      • 1970-01-01
      • 1970-01-01
      • 2015-08-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-23
      相关资源
      最近更新 更多