私はViewModelを持つビューをdatacontext(コードで設定)として持っています。私の見解では、私リストボックス内のボタン上のリストプリズム:指揮官<T>
<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>
のViewModel
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
の内側に言うとき、私は右、あなたがListBox
のItemTemplate
内側に言っていると思いますか?あなたのすべての
ありがとうskaffman – TheMar