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

发布于 2024-11-28 14:06:10 字数 2719 浏览 0 评论 0原文

我是 MVVM 新手,我遵循了 josh smith 文章,我努力开发我的第一次尝试。就我而言,我有一个具有主视图模型的主窗口:

var vm = new MainVM();
MainWindow window = new MainWindow();
window.DataContext = vm;

我有两个视图模型ItemSuppliersViewModelSuppliersViewModel绑定到两个视图ItemSuppliers,< code>SuppliersView 在主窗口 resourcedictionary 中使用 datatemplate 如下:

<DataTemplate DataType="{x:Type VM:ItemSuppliersViewModel}">
    <VV:ItemSuppliersView/>
</DataTemplate>
<DataTemplate DataType="{x:Type VM:SuppliersViewModel}">
    <VV:SuppliersView/>
</DataTemplate>

在主窗口中,我有一个显示项目列表的列表框绑定到:

<ListBox x:Name="ItemsListBox" ItemsSource="{Binding AllItems}" SelectedItem="{Binding     SelectedItem}" DisplayMemberPath="Item_Name" />

AllItems 是主视图模型公开的公共属性:

public IList<Item> AllItems { get { return (IList<Item>)_itemsRepository.FindAll(DetachedCriteria.For<Item>()); } }

当用户从列表框中选择一个项目时,将显示与该项目相关的一些数据列表,由 表示ItemSuppliers 视图模型和 ItemSuppliersView 并使用 itemscontrol 显示到网格中:

<Grid Margin="246,132,93,94">
        <ItemsControl ItemsSource="{Binding ItemSuppliersVM}" Margin="4"/>
    </Grid>

ItemSuppliersVM 在主视图模型中公开,如下所示:

ItemSuppliersViewModel itemSuppliersVM;
public ItemSuppliersViewModel ItemSuppliersVM
    {
        get
        {
            return _itemSuppliersVM;
        }
        set
        {
            _itemSuppliersVM = value;
            OnPropertyChanged("ItemSuppliersVM");
        }
    }

这是绑定到列表框所选项目的 selecteditem 属性:

    public Item SelectedItem
    {
        get
        {
            return _selectedItem;
        }
        set
        {
            _selectedItem = value;
            OnPropertyChanged("SelectedItem");
            ShowItemSuppliers();
        }
    }

创建 itemsuppliers 视图模型的 showItemSuppliers

void ShowItemSuppliers()
    {         
        _itemSuppliersVM = new ItemSuppliersViewModel(_itemsRepository, _selectedItem, new DateTime(2011, 03, 01), new DateTime(2011, 03, 30));
    }

问题是,当选择列表框中的任何项目时,没有任何反应,但是 itemsrepository code> 经过测试并且工作正常,当我遇到断点时,所有绑定都在工作,并且它遍历 selecteditem 属性,然后遍历 showitemsuppliers() 方法。

我认为问题出在这个方法上,那么出了什么问题,这个方法是在主窗口视图模型中实例化 ItemSuppliersViewModel 的正确方法吗?

I'm new to MVVM, I followed josh smith article and I am struggling developing my first attempt. In my case I have a main window that has a main view model:

var vm = new MainVM();
MainWindow window = new MainWindow();
window.DataContext = vm;

I have two viewmodels ItemSuppliersViewModel, SuppliersViewModel binded to two views ItemSuppliers, SuppliersView using datatemplate in the mainwindow resourcedictionary as follows:

<DataTemplate DataType="{x:Type VM:ItemSuppliersViewModel}">
    <VV:ItemSuppliersView/>
</DataTemplate>
<DataTemplate DataType="{x:Type VM:SuppliersViewModel}">
    <VV:SuppliersView/>
</DataTemplate>

In the main window I have a listbox displaying a list of items Binded to:

<ListBox x:Name="ItemsListBox" ItemsSource="{Binding AllItems}" SelectedItem="{Binding     SelectedItem}" DisplayMemberPath="Item_Name" />

the AllItems is a public property exposed by the Main view model:

public IList<Item> AllItems { get { return (IList<Item>)_itemsRepository.FindAll(DetachedCriteria.For<Item>()); } }

When the user select an item from the list box a list of some data related to this item is displayed represented by the ItemSuppliers View model and ItemSuppliersView and displayed into a grid using itemscontrol:

<Grid Margin="246,132,93,94">
        <ItemsControl ItemsSource="{Binding ItemSuppliersVM}" Margin="4"/>
    </Grid>

The ItemSuppliersVM is exposed in the main view model as following:

ItemSuppliersViewModel itemSuppliersVM;
public ItemSuppliersViewModel ItemSuppliersVM
    {
        get
        {
            return _itemSuppliersVM;
        }
        set
        {
            _itemSuppliersVM = value;
            OnPropertyChanged("ItemSuppliersVM");
        }
    }

Here is the selecteditem property that is binded to the listbox selected item:

    public Item SelectedItem
    {
        get
        {
            return _selectedItem;
        }
        set
        {
            _selectedItem = value;
            OnPropertyChanged("SelectedItem");
            ShowItemSuppliers();
        }
    }

the showItemSuppliers that creates the itemsuppliers view model:

void ShowItemSuppliers()
    {         
        _itemSuppliersVM = new ItemSuppliersViewModel(_itemsRepository, _selectedItem, new DateTime(2011, 03, 01), new DateTime(2011, 03, 30));
    }

The problem is that when selecting any item in the list box nothing happened, however the itemsrepository is tested and works fine, when I but a break point all bindings are working and it walks through the selecteditem property and then the showitemsuppliers() method.

I think the problem is in this method, so what's wrong and is this method is the right way to instantiate the ItemSuppliersViewModel in the mainwindow view model?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鹿! 2024-12-05 14:06:10

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

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

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

你的绑定应该可以工作。

You are setting the field directly, and aren't raising the PropertyChanged event. Without raising that event, the binding engine won't know that your property has changed. If you change

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

to

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

your binding should work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文