Заполнитель TextBox в C # WPF - Не удается связать свойство DepedencyProperty - PullRequest
0 голосов
/ 28 марта 2019

Я пытался создать свой TextBox с некоторым переменным текстом-заполнителем.Но свойство «PlaceholderText» не используется в Visual Brush - Label.

Текст заполнителя в <Label Content="{Binding PlaceholderText}" Foreground="LightGray" /> всегда пуст.

Когда я использую привязку из, привязка работает.Я попытался использовать привязку как текст: <TextBox Text="{Binding PlaceholderText}" >, и она отлично работает.

Почему бы не в <VisualBrush><Label Content="...">...

Я надеюсь, что кто-то может мне помочь.

<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;

        }
    }

1 Ответ

1 голос
/ 28 марта 2019

Вам необходимо связать с помощью "ElementName", но это более детально внутри Resources:

<Label 
    Content="{Binding Source={x:Reference phTextBox}, Path=PlaceholderText}"
            Foreground="LightGray" />

См. Также msdn forum .

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...