c# - 可以将参数传递给 WPF 用户控件吗?

标签 c# .net wpf mvvm prism

可以将值或参数传递给 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/

相关文章:

c# - 为什么 C# 编译器甚至不警告无限递归?

c# - 类似utorrent的TreeView控件

c# - 启动新窗口时如何关闭当前窗口(在代码中)

c# - 使用 SqlCommand.ExecuteNonQuery,如何获得超过 int 限制的受影响行数?

c# - 如果属性在构造函数中初始化,则似乎未设置 XAML 绑定(bind)

c# - Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式

c# - asp.net 如何重定向到根文件?

c# - 如何比较属于同一逻辑/可视树的两个 WPF 控件的相对 Z 顺序?

c# - CSS解析编码问题?

c# - 业务逻辑的正确位置在哪里?