Reputation: 644
I have a simple class and I want to simply create instances of my class via xaml. But I'm still getting errors like: "'Test' member is not valid because it does not have a qualifying type name."
UserControl1.xaml.cs:
namespace WpfTestApplication1
{
public class UserControl1 : UserControl
{
public string Test { get; set; }
public UserControl1()
{
}
}
}
UserControl1.xaml:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfTestApplication1">
<local:UserControl1>
<Setter Property="Test" Value="aaaa" />
</local:UserControl1>
</ResourceDictionary>
Please, help.
Upvotes: 0
Views: 46
Reputation: 18580
The way you have set the Property
on your UserControl
is not valid. You have set the Content
of your UserControl
by putting Setter
inside the nodes.
First define Test as DependencyProperty
if you want it to be binding target and then set it directly on UserControl as
<local:UserControl1 Test="aaaa"/>
Upvotes: 1
Reputation: 644
Finally I found it out. I defined the control in xaml as
<local:UserControl1 x:Key="xxx" Test="aaaa"/>
Without using setter and property statements, just by directly defining the property. Thanks for help!
Upvotes: 0
Reputation: 1568
As @us3r said...DependencyProperty is what you are looking for.
What you have to do is:
// Dependency Property
public static readonly DependencyProperty TestProperty =
DependencyProperty.Register( "Test", typeof(string),
typeof(UserControl1 ));
// .NET Property wrapper
public string Test
{
get { return GetValue(TestProperty ).; }
set { SetValue(TestProperty , value); }
}
Upvotes: 0