#c# #wpf #xaml #data-binding #binding
#c# #wpf #xaml #привязка к данным #привязка
Вопрос:
Я хочу определить стиль для текстового поля и в рамках стиля, который я хочу привязать к свойству, к которому применяется соответствующий стиль. Что-то вроде этого:
<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>
Ответ №1:
Вам просто нужно использовать RelativeSource
привязку для доступа к Text
свойству TextBox
.
<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>
Значение PlacementTarget
ToolTip
TextBox
здесь.
Ответ №2:
Вот рабочий пример:
<Style BasedOn="{StaticResource {x:Type TextBox}}" TargetType="{x:Type TextBox}">
<Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Path=Text}" />
</Style>
Ключ в том, чтобы использовать RelativeSource
привязку Self
.
Нет необходимости устанавливать триггер IsEnabled
, потому что a ToolTip
будет отображаться только для включенного элемента управления по умолчанию.
Комментарии:
1. Спасибо, я думаю, моя проблема в том, что у меня есть всплывающая подсказка в виде текстового блока: <Свойство триггера =»IsEnabled» Значение = «True»> <Свойство установки =»Всплывающая подсказка»> Значение> <TextBlock Text=»{Текст привязки, RelativeSource={RelativeSource Self}}»/> Значение> </Setter> </ Trigger> таким образом, он использует текстовый блок, а не само текстовое поле. Как я могу привязаться к родительскому текстовому полю в этом случае?