【问题标题】:Can parameters be passed to a WPF user control?参数可以传递给 WPF 用户控件吗?
【发布时间】:2014-03-10 02:46:23
【问题描述】:

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

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

在哪里

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

顺便问一下,属性的类型应该是Color还是Brush?

【问题讨论】:

    标签: c# .net wpf mvvm prism


    【解决方案1】:

    是的,您可以使用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}"/> 
    

    【讨论】:

    • 它没有用。我可以在 xaml 中看到值,但依赖属性没有得到值。它仍然为空。
    • 我建议您使用示例代码的方法。我使用相同的方法在用户控件之间传递数据。您可以根据需要对其进行增强。
    • @user2330678 你是对的,这将不起作用
    猜你喜欢
    • 2010-12-15
    • 2013-10-18
    • 1970-01-01
    • 2011-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多