Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can parameters be passed to a WPF user control?

Tags:

c#

.net

mvvm

wpf

prism

Can values or parameters be passed to a WPF user control? I am using MVVM pattern.

<local:SampleUserControlView Forecolor="{Binding ForeColor}"/> 

where

ForeColor is a property of Type Color or Brush in Viewmodel of the window hosting SampleUserControl View.

By the way, should the property be of type Color or Brush?

like image 570
user2330678 Avatar asked Feb 13 '23 19:02

user2330678


1 Answers

yes you can pass by using dependency properties. You can create dependency property in your usercontrol of type what you want to pass. Below is a small example which will show you how it works.

public static readonly DependencyProperty MyCustomProperty = 
DependencyProperty.Register("MyCustom", typeof(string), typeof(SampleUserControl));
public string MyCustom
{
    get
    {
        return this.GetValue(MyCustomProperty) as string;
    }
    set
    {
        this.SetValue(MyCustomProperty, value);
    }
}

And MyCustom you can set from your parent VM, like

<local:SampleUserControlView MyCustom="{Binding MyCustomValue}"/> 
like image 134
Mukesh Rawat Avatar answered Feb 23 '23 10:02

Mukesh Rawat