How to preform the following result in a my WPF project.
C# Code Behind
public class Test
{
public int a;
}
Custom Control Code Behind
public class myControl : Control
{
public Test myVar { get; set; }
}
Xaml Code
<myControl myVar.a=2/>
For Example, Using this Code like TextBlock Class in under code:
<TextBox TextBlock.FontFamily="12"/>
The example you give with TextBlock
<TextBox TextBlock.FontFamily="12"/>
is a different case, because TextBlock is not a property of TextBox.
Here you are setting a so called attached property.
Following your example, you would have to write the Test class as follows:
using System.Windows;
namespace WpfApp1
{
public class Test
{
public static readonly DependencyProperty aProperty
= DependencyProperty.RegisterAttached ( "a",
typeof(int),
typeof(Test),
new PropertyMetadata(0) ) ;
public static int Geta ( DependencyObject obj )
{
return (int)obj.GetValue(aProperty);
}
public static void Seta ( DependencyObject obj, int value )
{
obj.SetValue(aProperty, value);
}
}
}
You can then set it in XAML as follows:
<local:myControl local:Test.a="2" />
Note that I am not using your property myVar at all.
If this is what you want, I suggest you do some background reading on attached properties.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.