簡體   English   中英

具有提示作為自定義/用戶控件的WPF文本框

[英]WPF TextBox with Hint As Custom/User Control

因此,我正在嘗試設計盡可能簡單的UI,為此,我需要像Android一樣在文本框內提供提示。 我找到了解決該問題的許多解決方案(請參閱WPF中的水印/提示文本/占位符TextBox如何向WPF文本框添加提示文本? ),但是每種解決方案似乎都使​​用了大量XAML代碼,樣式,觸發器和排序方式。 我想做的是,我想擁有一個文本框子類,該子類具有一個HintText屬性,可以在任何地方使用它,但是到目前為止,我什至都沒有關閉。 這是我最近得到的:

<TextBox x:Class="MyProgram.CustomControls.HintTextBox"
             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" 
             mc:Ignorable="d" Text="ASDF"
             d:DesignHeight="174" d:DesignWidth="708">
    <TextBox.Resources>
        <VisualBrush x:Key="VB">
            <VisualBrush.Visual>
                <Label Content="{Binding Path=HintText}" Foreground="LightGray" FontSize="25"/>
            </VisualBrush.Visual>
        </VisualBrush>
    </TextBox.Resources>
    <TextBox.Style>
        <Style TargetType="TextBox">
            <Setter Property="Background" Value="{StaticResource VB}"/>
        </Style>
    </TextBox.Style>
</TextBox>

和:

public partial class HintTextBox : TextBox
{
    public HintTextBox()
    {
        InitializeComponent();
    }

    public static DependencyProperty HintTextProperty = DependencyProperty.Register("HintText", typeof(String), typeof(HintTextBox));
}

缺少觸發器定義,但這不是這里的主要問題。 我的主要問題是我似乎無法綁定HintText屬性。 我無法通過XAML分配它,並且由於某種原因也無法綁定到它。 我還嘗試綁定到TextBox自己的Text屬性,只是看它是否可以工作,而不能。 我究竟做錯了什么? 還是我完全將錯誤的樹吠叫?

編輯:我還需要一個密碼框相同的功能,否則就無法解決...為什么他們到底如何將文本框和密碼框分開呢?

問題在於,VisualBrush資源“ VB”可以在所有元素之間共享,並且標簽內容不能被綁定。

您可以嘗試使用帶有空文本提示的示例文本框

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM