多绑定命令参数

时间:2014-07-22 23:15:33

标签: wpf data-binding mvvm-light multibinding relaycommand

因此,每个listview项都有一个上下文菜单,listview绑定到用户列表。上下文菜单有一个子菜单,该子菜单绑定到可观察的用户状态集合。我希望能够将列表视图中的用户ID和上下文菜单中的新状态ID传递给我的更新命令参数。我只是查看MultiBindings并相信这可能是一个很好的长期解决方案,我可以在其他地方使用。这是一些代码:

用户视图中的ListView:

<ListView Background="Transparent" ItemsSource="{Binding UserList}" SelectionMode="Single">
    <ListView.Resources>
        <ContextMenu x:Key="Menu" DataContext="{Binding Path=PlacementTarget.DataContext, RelativeSource={RelativeSource Self}}">
            <MenuItem Name="UserID" Header="{Binding UserID}"/>
                <Separator></Separator>
                    <MenuItem Header="Status" ItemsSource="{Binding DataContext.UserStatus, RelativeSource={RelativeSource FindAncestor, AncestorType=UserControl}}"
                              DisplayMemberPath="Name" Name="StatusID">
                        <MenuItem.ItemContainerStyle>
                            <Style TargetType="MenuItem">
                                <Setter Property="Command" Value="{Binding DataContext.UpdateDriverStatus, RelativeSource={RelativeSource FindAncestor, AncestorType=UserControl}}" />
                                <Setter Property="CommandParameter" Value="{Binding}" />
                            </Style>

                        </MenuItem.ItemContainerStyle>
                        <MenuItem.CommandParameter>
                            <MultiBinding Converter="{StaticResource MultiBindConverter}">
                                <Binding ElementName="DriverID"></Binding>
                                <Binding ElementName="StatusID"></Binding>
                            </MultiBinding>
                        </MenuItem.CommandParameter>                        </MenuItem>
                </ContextMenu>
            </ListView.Resources>

            <ListView.ItemContainerStyle>
                <Style TargetType="{x:Type ListViewItem}">
                    <Setter Property="ContextMenu" Value="{StaticResource Menu}" />
                </Style>
            </ListView.ItemContainerStyle>
            <ListView.ItemTemplate>
                <ItemContainerTemplate>

                            <TextBlock Text="{Binding UserName}" >

                            </TextBlock>
                </ItemContainerTemplate>
            </ListView.ItemTemplate>
        </ListView>

用户虚拟机:

public class UsersPanelVM : ViewModelBase, INotifyPropertyChanged
{
    public ObservableCollection<UserPanelItem> UserList { get; set; }
    public ObservableCollection<UserStatusList> UserStatus { get; set; }

    private readonly IUserService _userService;
    public IUserService UserService { get { return this._userService; } }

    public UsersPanelVM(IUserService userService)
    {
        this._userService = userService;

        var model = this.UserService.GetUsers();
        this.UserList = model.Users;
        var statusmodel = this.UserService.GetUserStatus();
        this.UserStatus = statusmodel.UserStatus;

        this.UpdateUserStatus = new RelayCommand<UserStatusList>((s) => UpdateStatus(1,s));
    }

    //The 1, above, is hard coded to test the method call, but ideally that should be the selected UserID

    private void UpdateStatus(int ID, UserStatusList s)
    {

    }

    public RelayCommand<UserStatusList> UpdateUserStatus { get; private set; }

}

我很确定此刻我100%迷失了。

1 个答案:

答案 0 :(得分:2)

  1. 我没有必要这样做,因为您在对象模型中除了新状态之外的所有信息都

    将命令和UpdateStatus方法移动到UserPanelItem的类中,该类也应保存您的ID,然后您只需将命令更改为:

    new RelayCommand(param => UpdateStatus(ID, (UserStatusList)param))
    
  2. 如果你真的想这样做:你再次设置父CommandParameter的{​​{1}},其命令永远不会被使用,将其移至MenuItem - 容器样式中的CommandParameter&#39; Setter,即

    Value