可以将值或参数传递给 WPF 用户控件吗?我正在使用 MVVM 模式。
<local:SampleUserControlView Forecolor="{Binding ForeColor}"/>
where
ForeColor 是 Viewmodel 中 Type Color 或 Brush 的属性
托管 SampleUserControl 视图的窗口。
顺便问一下,属性应该是 Color 类型还是 Brush 类型?
是的,你可以使用dependency properties
。您可以在用户控件中创建您想要传递的类型的依赖属性。下面是一个小例子,将向您展示它是如何工作的。
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
您可以从父虚拟机进行设置,例如
<local:SampleUserControlView MyCustom="{Binding MyCustomValue}"/>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)