从在Silverlight XAML传递参数的构造函数 [英] Pass parameter to constructor from xaml in Silverlight

查看:313
本文介绍了从在Silverlight XAML传递参数的构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从Silverlight启用WCF服务DATAS并将其绑定到DataGrid的ItemSource。但我的视图模型的构造越来越参数。我使用MVVM。我想通过参数从XAML来构造函数。
我必须在这里补充?
这是在XAML的部分我在哪里设置页面的DataContext

 <导航:Page.DataContext> 
<虚拟机:StudentViewModel />
< /导航:Page.DataContext>



这是类的构造函数:

 公共StudentViewModel(字符串类型)
{
PopulateStudents(类型);
}



此外,这里是错误:




键入StudentViewModel'不可用作对象元素,因为它
是不公开或不定义一个公共的无参数的构造函数或
A型转换器。



解决方案

您只能在 WPF 使用默认参数的构造函数的错误消息指示。所以最好的办法就是让'类型'一的DependencyProperty ,并设置绑定了它,那么当它被设置打电话给你的 PopulateStudents()方法。

 公共类StudentViewModel:DependencyObject的
{
//参数的构造函数
公共StudentViewModel()
{
}

// StudentType依赖项属性
公共字符串StudentType
{
{返回(串)的GetValue(StudentTypeProperty); }
集合{的SetValue(StudentTypeProperty,值); }
}

公共静态只读的DependencyProperty StudentTypeProperty =
DependencyProperty.Register(StudentType的typeof(串)的typeof(StudentViewModel),新PropertyMetadata(DefaultType的StudentTypeChanged ));

//当类型更改然后填充学生
私有静态无效StudentTypeChanged(DependencyObject的D,DependencyPropertyChangedEventArgs E)
{
VAR studentVm = D为StudentViewModel;
如果(D == NULL)回报;

studentVm.PopulateStudents();
}

公共无效PopulateStudents()
{
//做的东西
}

//其它类的东西。 ..
}

的XAML

 <导航:Page.DataContext> 
<虚拟机:StudentViewModel StudentType ={结合YourBindingValueHere}/>
< /导航:Page.DataContext>


I am getting datas from Silverlight-enabled WCF service and binding it to the DataGrid ItemSource. But the constructor of my ViewModel is getting a parameter. I am using MVVM. ANd I want to pass parameter to constructor from xaml. What must I add here? This is the part in xaml where i am setting DataContext of page.

<navigation:Page.DataContext>
    <vms:StudentViewModel />
</navigation:Page.DataContext>

And this is the constructor of class:

 public StudentViewModel(string type)
    {
        PopulateStudents(type);
    }

Also, here is the error:

Type 'StudentViewModel' is not usable as an object element because it is not public or does not define a public parameterless constructor or a type converter.

解决方案

You can only instantiate an object in WPF using the default parameter-less constructor as the error message indicates. So your best bet is to make 'Type' a DependencyProperty and set a binding up for it, then when it is set call your PopulateStudents() method.

public class StudentViewModel : DependencyObject
{
    // Parameterless constructor
    public StudentViewModel()
    {
    }

    // StudentType Dependency Property
    public string StudentType
    {
        get { return (string)GetValue(StudentTypeProperty); }
        set { SetValue(StudentTypeProperty, value); }
    }

    public static readonly DependencyProperty StudentTypeProperty =
        DependencyProperty.Register("StudentType", typeof(string), typeof(StudentViewModel), new PropertyMetadata("DefaultType", StudentTypeChanged));

    // When type changes then populate students
    private static void StudentTypeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var studentVm = d as StudentViewModel;
        if (d == null) return;

        studentVm.PopulateStudents();
    }

    public void PopulateStudents()
    {
        // Do stuff
    }

    // Other class stuff...
}

Xaml

<navigation:Page.DataContext>
    <vms:StudentViewModel StudentType="{Binding YourBindingValueHere}" />
</navigation:Page.DataContext>

这篇关于从在Silverlight XAML传递参数的构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆