.net - Prism :指挥官<T>

标签 .net mvvm prism

我有一个 ViewModel 作为数据上下文的 View (在代码中设置)。在我看来,我有一个 list

<UserControl x:Class="ZPOS.Modules.Menu.Views.DepartmentView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:prism="clr-namespace:Microsoft.Practices.Composite.Presentation.Commands;assembly=Microsoft.Practices.Composite.Presentation">
    <Grid>
<Grid.Background>
            <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                <GradientStop Color="#FF9CA48A"/>
                <GradientStop Color="#FFFFFFFF" Offset="1"/>
                <GradientStop Color="#FF90A85C" Offset="0.5"/>
            </LinearGradientBrush>
        </Grid.Background>
        <ListBox ItemsSource="{Binding Departments}"
                 SelectionChanged="ListBox_SelectionChanged">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        <Grid.Background>
                            <LinearGradientBrush>
                                <GradientStop Color="Black" Offset="0"/>
                                <GradientStop Color="White" Offset="1"/>
                            </LinearGradientBrush>
                        </Grid.Background>
                        <Grid.RowDefinitions>
                            <RowDefinition/>
                            <RowDefinition/>
                        </Grid.RowDefinitions>
                        <Button Height="Auto" HorizontalAlignment="Left" Margin="1,1,1,1" Grid.Column="0" Grid.Row="0" Content="{Binding Path=Name}"  
                                prism:Click.Command="{Binding displayMenubyCategory}" VerticalAlignment="Bottom" Width="Auto"/>
                        <TextBlock  Grid.Column="0" Grid.Row="1"   Text="{Binding Path=Note}" />
                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
        <Button Height="Auto" HorizontalAlignment="Left" Margin="1,1,1,1" Grid.Column="0" Grid.Row="0" Content="{Binding Path=Name}" prism:Click.Command="{Binding displayMenubyCategory}" VerticalAlignment="Bottom" Width="Auto"/>

    </Grid>
</UserControl>

View 模型
using System;
using System.ComponentModel;
using Microsoft.Practices.Composite.Events;
using System.Collections.Generic;
using ZPOS.Infrastructure;
using ZPOS.Objects;
using System.Collections.ObjectModel;
using ZPOS.Modules.Menu.Views;
using ZPOS.Contracts;
using Microsoft.Practices.Composite.Presentation.Commands;
namespace ZPOS.Modules.Menu.PresentationModels
{
    public class DepartmentViewModel : IDepartmentViewModel, INotifyPropertyChanged
    {


        private readonly IEventAggregator eventAggregator;
        private string _message;
        IMenuService service;

        public DelegateCommand<POSDepartment> displayMenubyCategory { get; private set; } 

        public string Name { get; set; }



        private ObservableCollection<POSDepartment> deptItems;
        public ObservableCollection<POSDepartment> Departments
        {
            get { return deptItems; }
            private set
            {
                if (deptItems != value)
                {
                    deptItems = value;
                    PropertyChanged(this, new PropertyChangedEventArgs("deptItems"));
                }
            }
        }

        public string Message
        {
            get { return _message; }
            set
            {
                if (_message != value)
                {
                    _message = value;
                    PropertyChanged(this, new PropertyChangedEventArgs("deptItems"));
                }
            }
        }



        public IDepartmentView View { get; private set; }

        public event PropertyChangedEventHandler PropertyChanged = delegate { };

        private void NotifyPropertyChanged(string propertyName)
        {
            var handler = this.PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }





        public DepartmentViewModel(IDepartmentView deptView, IEventAggregator eventAggregator, IMenuService service)
        {
            this.View = deptView;
            this.View.Model = this;
            this.eventAggregator = eventAggregator;
            this.service = service;
            this.Name = "View for DepartmentModel";

            this.eventAggregator.GetEvent<DepartmentSelectionChangedEvent>().Subscribe(departmentSelectionChanged);
            displayMenubyCategory = new DelegateCommand<POSDepartment>(ExecuteCommand1, CanExecuteCommand1);

            PopulateDepartmentItems();
        }


        private void ExecuteCommand1(POSDepartment commandParameter)
        {
        }

        private bool CanExecuteCommand1(POSDepartment commandParameter)
        {
            return true;
        }

        public void departmentSelectionChanged(POSDepartment item)
        {
            this.Message = item.Name;
        }



        private void PopulateDepartmentItems()
        {
            try
            {

                List<POSDepartment> items = service.GetAllDepartments();

                deptItems = new ObservableCollection<POSDepartment>(items);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }


    }
}

单击 listBox 内的 Button 不会触发该命令。

如果我将相同的按钮放在列表框之外,则会调用 delage。

难道我做错了什么?

有没有更好的办法。我还是 Prism 的新手。当命令被触发时,我还想传递参数(列表框项的数据上下文)。

谢谢你们

最佳答案

正如 Kent 提到的,您可以使用 RelativeSource。第一次如何使用它并不总是很清楚,所以这里有一个示例供您使用。我认为这会起作用(为简洁起见,删除了一些按钮属性):

<Button prism:Click.Command="{Binding 
RelativeSource={RelativeSource FindAncestor, 
AncestorType={x:Type UserControl}}, Path=DataContext.displayMenubyCategory}" />

这个应该可以的。它假定您将 DataContext 设置为您的父 ViewModel。

关于.net - Prism :指挥官<T>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1364260/

相关文章:

用于音乐编程的 .NET API?

C# RegEx 同时匹配字符串开头和单词开头

.net - 跨 azure serviceFabric 服务共享文件

c# - ObservableCollection设置后不返回新数据

wpf - wpf的 Prism vs mvvm灯

wpf - 在 Wpf 应用程序中使用 Prism 进行导航

c# - 使用标题列表,用于切换

c# - 如何将 IsSelected for MVVM 移出业务对象?

mvvm - 您如何处理 MVVM 中的更改跟踪?

c# - 通过索引号 wpf mvvm 在 listview itemsource 中搜索值