[英]Placeholder TextBox in C# WPF - Can't bind the DepedencyProperty
I tried to build my a TextBox with some variable placeholder text. 我试图用一些可变的占位符文本构建一个TextBox。 But the property "PlaceholderText" ist not used in the Visual Brush - Label.
但是,“ Visual Brush-Label”中未使用属性“ PlaceholderText”。
The placeholder text in <Label Content="{Binding PlaceholderText}" Foreground="LightGray" />
is always empty. <Label Content="{Binding PlaceholderText}" Foreground="LightGray" />
的占位符文本始终为空。
When I use the binding out of the , the binding works. 当我使用中的绑定时,绑定有效。 I tried to use the binding as Text:
<TextBox Text="{Binding PlaceholderText}" >
and it works perfectly. 我尝试将绑定用作文本:
<TextBox Text="{Binding PlaceholderText}" >
,它可以完美地工作。
Why not inside the <VisualBrush><Label Content="...">...
为什么不在
<VisualBrush><Label Content="...">...
I hope someone can help me. 我希望有一个人可以帮助我。
<TextBox x:Class="CustomerPro.Common.Controls.PlaceholderTextBox"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:CustomerPro.Common.Controls"
mc:Ignorable="d"
d:DesignHeight="50" d:DesignWidth="800" Name="phTextBox">
<TextBox.Style>
<Style TargetType="{x:Type TextBox}">
<Style.Resources>
<VisualBrush x:Key="PHBoxBackground" AlignmentX="Left" AlignmentY="Center" Stretch="None">
<VisualBrush.Visual>
<Label Content="{Binding PlaceholderText}" Foreground="LightGray" />
</VisualBrush.Visual>
</VisualBrush>
</Style.Resources>
<Style.Triggers>
<Trigger Property="Text" Value="">
<Setter Property="Background" Value="{StaticResource PHBoxBackground}" />
</Trigger>
<Trigger Property="Text" Value="{x:Null}">
<Setter Property="Background" Value="{StaticResource PHBoxBackground}" />
</Trigger>
<Trigger Property="IsKeyboardFocused" Value="True">
<Setter Property="Background" Value="White" />
</Trigger>
</Style.Triggers>
</Style>
</TextBox.Style>
</TextBox>
public partial class PlaceholderTextBox : TextBox
{
#region PlaceholderText
/// <summary>
/// Gets or sets the PlaceholderText which is displayed next to the field
/// </summary>
public String PlaceholderText
{
get { return (String)GetValue(PlaceholderTextProperty); }
set { SetValue(PlaceholderTextProperty, value); }
}
/// <summary>
/// Identified the PlaceholderText dependency property
/// </summary>
public static readonly DependencyProperty PlaceholderTextProperty = DependencyProperty.Register("PlaceholderText", typeof(string), typeof(PlaceholderTextBox), new PropertyMetadata("Type some Text"));
#endregion
public PlaceholderTextBox()
{
InitializeComponent();
this.phTextBox.DataContext = this;
}
}
You need to bind by " ElementName
" but that is more elaborated inside the Resources
: 您需要通过“
ElementName
”进行绑定,但这在Resources
中有更详细的说明 :
<Label
Content="{Binding Source={x:Reference phTextBox}, Path=PlaceholderText}"
Foreground="LightGray" />
See also msdn forum . 另请参见msdn论坛 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.