简体   繁体   English

如何将水印和自定义属性都分配给TextBox的Text属性? (MahApps)

[英]How to assign both Watermark and a custom property to a TextBox's Text property? (MahApps)

I'm having problems with creating a suitable watermark in my TextBox. 我在TextBox中创建合适的水印时遇到问题。

I use MahApps in my project, and as long as I don't bind 'Text' to my custom property, all works fine. 我在项目中使用MahApps,只要不将“文本”绑定到自定义属性,一切都可以正常工作。

But I need to trace the changes in the TextBox, and so I bind the Text property like this: 但是我需要跟踪TextBox中的更改,因此我像这样绑定Text属性:

<TextBox controls:TextboxHelper.Watermark="Enter text here..." Text="{Binding Path=MyProperty}" />

In this case, the watermark property stops working, the watermark text doesn't disappear when I start typing. 在这种情况下,水印属性停止工作,当我开始键入时水印文本不会消失。

How can it be helped? 如何提供帮助? Thanks! 谢谢!

UPDATE Here's the window of the sample made by har07 . 更新这是har07制作的示例的窗口。 In the lower unbound TextBox the watermark works as expected. 在下部未绑定的TextBox中,水印按预期方式工作。 However, when I try to type smth in the first TextBox, the watermark is still there. 但是,当我尝试在第一个TextBox中键入smth时,水印仍然存在。 在此处输入图片说明

UPDATE2 Just in case someone will make the same mistake - appeared I was indeed setting my property in code while initializing the window, and that was the reason the watermark wasn't working. UPDATE2以防万一有人会犯同样的错误-似乎我在初始化窗口时确实在代码中设置了我的属性,这就是水印不起作用的原因。 Now, thanks to har07, all is fine. 现在,感谢har07,一切都很好。

As I said in comment, there shouldn't be any problem setting watermark along with Text binding. 正如我在评论中所说,设置水印和Text绑定应该没有任何问题。 I made simple test using 2 textboxes, one with Text property bound and the other not bound. 我使用2个文本框进行了简单的测试,一个Text框绑定了Text属性,另一个未绑定。 Both are showing same behavior : watermark text replaced by typed text, and get dimmed upon textbox lost focus . 两者都表现出相同的行为: 水印文本由键入的文本替换,并且在文本框失去焦点时变暗

//View
<Controls:MetroWindow x:Class="WpfMahApps.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
        xmlns:local="clr-namespace:WpfMahApps"
        Title="StackOverflow" Height="500" Width="625"
        WindowStartupLocation="CenterScreen">
    <Grid>
        <Grid.DataContext>
            <local:Person/>
        </Grid.DataContext>
        <StackPanel>
            <TextBox Text="{Binding Name}" Controls:TextboxHelper.Watermark="Enter text here...."/>
            <TextBox Controls:TextboxHelper.Watermark="Enter text here too...."/>
        </StackPanel>
    </Grid>
</Controls:MetroWindow>

//Model (I'm using MvvmLight for implementation of INPC)
public class Person : ObservableObject
{
    private string _name = "Default Name";
    public String Name
    {
        get { return _name; }
        set
        {
            _name = value;
            RaisePropertyChanged(() => Name);
        }
    }
}

Download test project 下载测试项目

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM