棱镜:指挥代表<t> </t>

时间:2009-09-01 19:35:45

标签: .net mvvm prism

我有一个View with ViewModel作为datacontext(在代码中设置)。在我看来,我有一个清单

<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>

视图模型

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的新手。我还想在命令被触发时传递参数(列表框项的数据上下文)。

谢谢大家

2 个答案:

答案 0 :(得分:3)

正如肯特所说,你可以使用RelativeSource。第一次使用它并不总是很清楚,所以这里有一个样品给你。我认为这样可行(为简洁起见,删除了一些按钮属性):

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

这应该这样做。它假定您将DataContext设置为父ViewModel。

答案 1 :(得分:2)

当你在ListBox内说时,我想你在ItemTemplate的{​​{1}}内说,对吧?

ListBox适用于为ItemTemplate生成的每个项目。每个生成的项都有一个ListBox设置为生成它的数据项。因此,该数据项需要一个名为DataContext的属性,否则绑定将失败。查看Visual Studio中的输出窗口并检查绑定错误。

displayMenubyCategory移到Button之外意味着不同的ListBox(您的视图模型),这意味着绑定将成功,因此一切都会正常工作。

您的选择包括:

  1. 向您的数据类添加DataContext属性,该属性可能会调用主视图模型的相同属性。
  2. 更改您的绑定以查看当前displayMenubyCategory之外的内容(请参阅RelativeSource)。