2015-09-04 66 views
2

我有這種自定義控件,但不能從我的主要XAMLWPF自定義用戶控件 - 「會員X無法識別或訪問」

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

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

    public string RegisterTitle 
    { 
     get { return this.LabelRegisterTitle.Content.ToString(); } 
     set { this.LabelRegisterTitle.Content = value; } 
    } 

訪問它的成員,這是什麼我在主XAML中出現錯誤,指出「成員RegisterTitle無法識別或訪問」:

<Controls:RegisterTextBox RegisterTitle="This Is A Test"/> 

我觀看了一些YouTube視頻,這正是他們如何做到的,出於某種原因,這對他們有效。 請諮詢這裏可能是什麼問題。 謝謝!

回答

2

試着改變你的財產將被聲明爲Dependency Properties

這將有助於它具有示例代碼 - Why am I seeing a 「member is not recognized or is not accessible」 error on my WPF User Control?

更新

你的代碼工作正常,我沒有使用依賴屬性,所以幾件事情可以嘗試:

  • 在你的控制下,請確保您有</UserControl>
  • 變化Controls:RegisterTextBox結束它使用較低的情況下,「C」,所以controls:RegisterTextBox
+0

謝謝!我可能不遵循它,所以它仍然不適合我。我做了正確的更改:'

+0

我剛剛嘗試了您的代碼而未使用「依賴屬性」,並且工作正常。只是爲了檢查,你在用戶控制的最後有''?此外,更改''使用小寫'c',所以'' –

+0

是的,我做最後有'',錯過複製/粘貼。但由於某種原因,我仍然無法完成它的工作。順便說一句,爲什麼要將小寫'c'封裝在'FireflyMmwDiagnostics.Controls'命名空間中?它不會以較低的'c'編譯。 – SYB

2

您可能需要以重建爲它看到RegisterTitle

如果這不起作用,請確保您在XAML中定義了Controls在您的項目中,否則它肯定無法看到它!

例如:

<Window xmlns:Controls="clr-namespace:FireflyMmwDiagnostics.Controls.RegisterTextBox"> 
+0

謝謝,我做到了..我會嘗試上面的建議 – SYB

相關問題