【问题标题】:Sending an object with a command parameter发送带有命令参数的对象
【发布时间】:2014-08-01 00:29:26
【问题描述】:

我正在使用 PRISM 使用 MVVM 设计模式开发我的 Windows Phone 应用程序。我需要通过委托命令将 LongListSelector 中的 SelectedItem 对象传递到我的方法中。

我可以做到。问题是,我传入了错误的对象。不知道是设计问题还是装订不当。

我需要对象是专辑对象。相反,我得到的是 null 或我的 ViewModel。 (我已经更改了几次代码,这是我唯一能得到的东西。)

XAML

<phone:LongListSelector x:Name="AlbumList" ItemsSource="{Binding Albums}"
                 Margin="10,0,0,0" LayoutMode="Grid" GridCellSize="200, 200"
                 ItemTemplate="{StaticResource AlbumTemplate}"
                                toolkit:TiltEffect.IsTiltEnabled="True"
                                >
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="SelectionChanged">
                    <i:InvokeCommandAction Command="{Binding DataContext.SelectAlbumCommand, ElementName=ContentPanel}"
                                        CommandParameter="{Binding}"/>   
                </i:EventTrigger>
            </i:Interaction.Triggers>
        </phone:LongListSelector>

视图模型

private ObservableCollection<Album> _albums;
    public ObservableCollection<Album> Albums
    {
        get { return _albums; }
        set
        {
            if (value != null)
            {
                _albums = value;
                NotifyPropertyChanged();
            }
        }
    }

    private Album _selectedAlbum;
    public Album SelectedAlbum
    {
        get { return _selectedAlbum; }
       // code removed as it is not needed; the object is null when trying to set.
}

        public void AlbumSelected(object p)
    {

        App.Dispatcher.BeginInvoke(() =>
            {
                SelectedAlbum = (Album)p;
            });

        ////Navigate("/Views/PhotosListPage.xaml");
    }

//command that takes an object as parameter.
            _selectAlbumCommand = new DelegateCommand<object>(this.AlbumSelected);

【问题讨论】:

    标签: c# mvvm windows-phone prism


    【解决方案1】:

    如果您只想通过SelectAlbumCommand 设置SelectedAlbum,为什么不尝试将SelectedItem 绑定到SelectedAlbum 呢?

    <phone:LongListSelector x:Name="AlbumList" ItemsSource="{Binding Albums}" 
     SelectedItem="{Binding SelectedAlbum}" />
    

    如果您确实想将SelectedItem 传递给SelectedAlbumCommand(出于其他原因),您应该将CommandParameter 绑定到SelectedItemLongListSelector

     <i:InvokeCommandAction Command="{Binding DataContext.SelectAlbumCommand, ElementName=ContentPanel}" CommandParameter="{Binding ElementName=AlbumList, Path=SelectedItem}"/>   
    

    【讨论】:

    • +1,但在您的第一个示例中,SelectedItem 绑定必须具有 Mode=TwoWay (?)
    • 是的.. TwoWay.. 我认为 LongListSelector 的 SelectedItem 属性的默认值是 TwoWay.. 再次指定没有坏处
    • 谢谢,但是当我将 SelectedAlbum 直接绑定到 SelectedItem 时,当我单击一个项目时,对象 p 仍然显示为 null 以及您更新的 CommandParameter 代码。
    【解决方案2】:

    显然,您不能为此使用 LongListSelector。我不得不把它改成一个列表框,它工作得很好。

    如果我更努力地搜索,我会找到这个:How to select an item in LongListSelector using the MVVM-pattern?

    还有这个:WP8 LongListSelector SelectedItem not bindable

    【讨论】:

    • 好发现.. 正要向你推荐同样的东西
    猜你喜欢
    • 2011-04-05
    • 2019-07-30
    • 1970-01-01
    • 1970-01-01
    • 2015-12-23
    • 1970-01-01
    • 2012-03-15
    • 1970-01-01
    • 2015-04-28
    相关资源
    最近更新 更多