Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用DependencyProperty在文本框(UWP)上显示占位符文本_C#_Xaml_Uwp_Dependency Properties - Fatal编程技术网

C# 使用DependencyProperty在文本框(UWP)上显示占位符文本

C# 使用DependencyProperty在文本框(UWP)上显示占位符文本,c#,xaml,uwp,dependency-properties,C#,Xaml,Uwp,Dependency Properties,我有一个自定义用户控件,它只包含一个文本框,我希望能够将占位符文本bing到该文本框。我的用户控制代码如下 <UserControl x:Class="Proj.Editors.EditTextControl" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:M

我有一个自定义用户控件,它只包含一个文本框,我希望能够将占位符文本bing到该文本框。我的用户控制代码如下

<UserControl
x:Class="Proj.Editors.EditTextControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:MemberSuiteConsoleApp.Controls.Editors"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">

<Grid>
    <TextBox x:Name="txtbox"  Width="300" PlaceholderText="{Binding ElementName=txtbox, Path=DataContext.PlaceholderText}"></TextBox>
</Grid>
我试图在我的页面中使用这个UserControl,就像在我的页面中一样

 <Grid>
    <editors:EditTextControl PlaceholderText="My placeholder" Height="400"></editors:EditTextControl>
</Grid>


但由于某些原因,文本框中没有显示占位符文本,我这里缺少什么?

您可以使用x:Bind。我试过了,而且成功了

<TextBox x:Name="txtbox"  Width="300" PlaceholderText="{x:Bind PlaceholderText, Mode=OneWay}"></TextBox>


通常我会使用转换器生成占位符文本。VM的值为null、空字符串或类似值将被转换为占位符文本。@Christopher占位符是一个开始,我还有很多其他属性来建立依赖关系,这就是为什么我要先解决一个问题。因此,我可以按照其他方法进行操作。任何东西都不会阻止您在多个绑定上使用一个转换器。只要所有这些属性都以相同的方式表示“无值”或“等待用户输入”,就可以了。在您的第一条评论中,您谈到VM的值为NULL,但在我的例子中,我直接在XAML本身中分配占位符文本,它会受到VM值的影响吗?我甚至不知道它有一个属性,但我应该猜到。当然,您不需要转换器:至少在我阅读属性描述时,只要值不变,就应该显示占位符文本。可能的意思是“改变了最初检索到的绑定”。我不确定它对用户删除输入之类的东西会有什么反应(即使这不会恢复准确的起始值)。
<TextBox x:Name="txtbox"  Width="300" PlaceholderText="{x:Bind PlaceholderText, Mode=OneWay}"></TextBox>