Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从另一个视图模型实例化和显示视图模型_C#_.net_Wpf_Data Binding_Mvvm - Fatal编程技术网

C# 如何从另一个视图模型实例化和显示视图模型

C# 如何从另一个视图模型实例化和显示视图模型,c#,.net,wpf,data-binding,mvvm,C#,.net,Wpf,Data Binding,Mvvm,我是MVVM的新手,我紧随其后,正在努力开发我的第一次尝试。在我的例子中,我有一个主窗口,其中有一个主视图模型: var vm = new MainVM(); MainWindow window = new MainWindow(); window.DataContext = vm; public IList<Item> AllItems { get { return (IList<Item>)_itemsRepository.FindAll(DetachedCrite

我是MVVM的新手,我紧随其后,正在努力开发我的第一次尝试。在我的例子中,我有一个主窗口,其中有一个主视图模型:

var vm = new MainVM();
MainWindow window = new MainWindow();
window.DataContext = vm;
public IList<Item> AllItems { get { return (IList<Item>)_itemsRepository.FindAll(DetachedCriteria.For<Item>()); } }
void ShowItemSuppliers()
    {         
        _itemSuppliersVM = new ItemSuppliersViewModel(_itemsRepository, _selectedItem, new DateTime(2011, 03, 01), new DateTime(2011, 03, 30));
    }
我有两个视图模型
ItemSuppliersViewModel
SuppliersViewModel
绑定到两个视图
ItemSuppliers
SuppliersView
在主窗口
resourcedictionary
中使用
datatemplate
,如下所示:

<DataTemplate DataType="{x:Type VM:ItemSuppliersViewModel}">
    <VV:ItemSuppliersView/>
</DataTemplate>
<DataTemplate DataType="{x:Type VM:SuppliersViewModel}">
    <VV:SuppliersView/>
</DataTemplate>
当用户从列表框中选择一个项目时,将显示与该项目相关的一些数据列表,这些数据由
项目供应商
视图模型和
项目供应商视图表示,并使用
项目控制
显示在网格中:

<Grid Margin="246,132,93,94">
        <ItemsControl ItemsSource="{Binding ItemSuppliersVM}" Margin="4"/>
    </Grid>
以下是绑定到listbox选定项的selecteditem属性:

    public Item SelectedItem
    {
        get
        {
            return _selectedItem;
        }
        set
        {
            _selectedItem = value;
            OnPropertyChanged("SelectedItem");
            ShowItemSuppliers();
        }
    }
创建itemsuppliers视图模型的
showItemSuppliers

var vm = new MainVM();
MainWindow window = new MainWindow();
window.DataContext = vm;
public IList<Item> AllItems { get { return (IList<Item>)_itemsRepository.FindAll(DetachedCriteria.For<Item>()); } }
void ShowItemSuppliers()
    {         
        _itemSuppliersVM = new ItemSuppliersViewModel(_itemsRepository, _selectedItem, new DateTime(2011, 03, 01), new DateTime(2011, 03, 30));
    }
问题是,当在列表框中选择任何项时,什么都没有发生,但是,
itemsrespository
经过测试,工作正常,当我只有一个断点时,所有绑定都在工作,它会遍历
selecteditem
属性,然后是
showitemsuppliers()
方法


我认为问题出在这个方法中,那么这个方法错在哪里?它是在主窗口视图模型中实例化
ItemSuppliersViewModel
的正确方法?

您直接设置字段,而不是引发
PropertyChanged
事件。如果不引发该事件,绑定引擎将不会知道您的属性已更改。如果你改变

_itemSuppliersVM = new ItemSuppliersViewModel(_itemsRepository, _selectedItem, new DateTime(2011, 03, 01), new DateTime(2011, 03, 30));

你的装订应该能用。

你的装订有错