Why am I seeing a "member not recognized or unreachable" error in my WPF user control?

I have a user control with a public property that I would like to set in XAML. Here it is below.

Testcontrol.xaml

<UserControl x:Class="Scale.Controls.TestControl" 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"> 

TestControl.xaml.cs

 using System.Windows.Controls; namespace MyProject.Controls { public partial class TestControl : UserControl { public string TestMe { get; set; } public TestControl() { InitializeComponent(); } } } 

Then, in my MainWindow.xaml file, I try to include this:

 <controls:TestControl TestMe="asdf" /> 

However, although Visual Studio automatically populates the TestMe property, I then see things with a squiggly expression that says, "The Test Me member is not recognized or unavailable," as shown below.

The member "Test Me" is not recognized or is not accessible

I could have sworn I used to do something similar in other projects. How can I access (e.g. set) public properties via XAML as follows?

+8
c # wpf xaml
source share
5 answers

You need to declare your property as Dependency Properties

 namespace MyProject.Controls { public partial class TestControl : UserControl { //Register Dependency Property public static readonly DependencyProperty TestMeDependency = DependencyProperty.Register("MyProperty", typeof(string), typeof(TestControl)); public string MyCar { get { return (string)GetValue(TestMeDependency); } set { SetValue(TestMeDependency, value); } } public TestControl() { InitializeComponent(); } } } 
+3
source share

Visual studio 2017

I had exactly the same problem. He was going to one day ... and then it was not. I did not use DependencyProperty, which is not needed, as mentioned above. Properties appeared in Intellisense, but when you type the same message. I cleaned, built, rebuilt, restarted VS, rebooted, etc. All to no avail.

The last sampler ... I removed all the insult attributes and got a clean compilation. Then I put them back and compiled. I really did not expect this. Somehow V.S. tightened his panties.

+11
source share

Change the build target from AnyCPU to x86 or x64. It is not clear why AnyCPU does not work.

0
source share

instead of setting several dependency properties for the control, I would use reflection.

  public static readonly DependencyProperty UserControlProperty = DependencyProperty.Register("UserControl", typeof(object), typeof(CustomUserControl), new PropertyMetadata(null)); public object UserControl { get { return GetValue(UserControlProperty); } set { SetValue(UserControlProperty, value); } } 
-one
source share

In my case, there are no original errors, after I changed my class, there are some errors in my class, and then the Xaml Error member is not recognized show. After solving the errors in my class, I pass the projects, all the projects are built without errors, but the errors are still displayed in the Error List Window . In the end, I restart Visual Studio, the errors disappear.

-one
source share

All Articles