参数可以传递给WPF用户控件吗?

可以将值或参数传递给WPF用户控件吗? 我正在使用MVVM模式。

 

哪里

ForeColor是托管SampleUserControl View的窗口的Viewmodel中的Type Color或Brush的属性。

顺便说一下,属性应该是Color还是Brush?

是的,你可以使用dependency properties传递。 您可以在您要传递的类型的usercontrol中创建依赖项属性。 下面是一个小例子,它将向您展示它是如何工作的。

 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); } } 

您可以从父虚拟机设置MyCustom ,例如