Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to pass parameter in wpf control constructor?

Tags:

c#

wpf

xaml

I have written my control and trying to pass parameter for additional initialization but there are errors =( (tHE TYPE Ajustcontrol could not have a name attribut ). How to pass data correctly? this is my code in c#:

public AjustControl(BaoC input)
        {
            InitializeComponent();

            populateAdjustControl(input);

        }

Error:Error 15 The type 'AjustControl' cannot have a Name attribute. Value types and types without a default constructor can be used as items within a ResourceDictionary. Line 470 Position 26. D:\Prj\aaa\MainWindow.xaml 470 26 Studio

like image 721
Papa John Avatar asked Dec 26 '11 14:12

Papa John


1 Answers

So, as the error says. You cannot have controls without parameterless constructor in xaml. You can still add one if you want to instantiate it from code, but xaml won't call that constructor.

public AjustControl(BaoC input) : this()
{
    populateAdjustControl(input);
}

public AjustControl()
{
    InitializeComponent();
}

However, if you are asking to add custom property to your control, you can add a DependancyProperty.

public static readonly DependencyProperty NameProperty= 
    DependencyProperty.Register(
    "Name", typeof(string),
...
    );
public string Name
{
    get { return (string)GetValue(NameProperty); }
    set { SetValue(NameProperty, value); }
}

After this, you can use your control like

<custom:AjustControl Name="something" />
like image 177
Tomislav Markovski Avatar answered Oct 11 '22 03:10

Tomislav Markovski