可以将值或参数传递给 WPF 用户控件吗?我正在使用 MVVM 模式。
<local:SampleUserControlView Forecolor="{Binding ForeColor}"/>
在哪里
ForeColor is a property of Type Color or Brush in Viewmodel of the window hosting SampleUserControl View.
顺便说一下,属性应该是颜色类型还是画笔类型?
最佳答案
是的,你可以通过使用 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);
}
}
和
MyCustom
你可以从你的父虚拟机设置,比如<local:SampleUserControlView MyCustom="{Binding MyCustomValue}"/>
关于c# - 可以将参数传递给 WPF 用户控件吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21716701/