WPF 自定义用户控件 - “成员 X 无法识别或无法访问”

SYB*_*SYB 6 c# wpf xaml

我有这个自定义控件,但无法从我的主 XAML 访问它的成员

<UserControl x:Class="FireflyMmwDiagnostics.Controls.RegisterTextBox"
         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" 
         d:DesignHeight="300" d:DesignWidth="300">
<Grid>
    <Label Name="LabelRegisterTitle"/>
</Grid>
Run Code Online (Sandbox Code Playgroud)

public partial class RegisterTextBox : UserControl
{
    public RegisterTextBox()
    {
        InitializeComponent();
    }

    public string RegisterTitle
    {
        get { return this.LabelRegisterTitle.Content.ToString(); }
        set { this.LabelRegisterTitle.Content = value; }
    }
Run Code Online (Sandbox Code Playgroud)

这就是我在 Main XAML 中遇到的错误,指出“成员 RegisterTitle 无法识别或无法访问”:

<Controls:RegisterTextBox RegisterTitle="This Is A Test"/>
Run Code Online (Sandbox Code Playgroud)

我看了几个 YouTube 视频,这正是他们所做的,并且出于某种原因对他们有用。请就此处可能出现的问题提供建议。谢谢!

Stu*_*art 3

尝试将您的财产更改为Dependency Properties

\n\n

这应该会有所帮助,因为它有示例代码 -为什么我在 WPF 用户控件上看到 \xe2\x80\x9cmember is not recognize or is notaccessible\xe2\x80\x9d 错误?

\n\n

更新

\n\n

您的代码在不使用依赖项属性的情况下对我来说工作正常,因此可以尝试以下几件事:

\n\n
    \n
  • 在你的控制之下,确保你以</UserControl>
  • \n
  • 改为Controls:RegisterTextBox使用小写\'c\',所以controls:RegisterTextBox
  • \n
\n