【问题标题】:How to get selected item of a listbox within a listbox如何在列表框中获取列表框的选定项
【发布时间】:2023-03-11 00:41:01
【问题描述】:

我在另一个列表框的 dataTemplate 中有一个列表框。我可以获取外部列表框的选定项,但我一直试图获取内部列表框的选定项(名称:“ListBoxLetter”),没办法..

这是我的 xaml:

        <ListBox x:Name="ListBoxOut" ItemsSource="{Binding Letters}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel>

                    <StackPanel Width="500" Height="60" Background="#16C8DB">
                        <TextBlock Text="{Binding Date}" />
                    </StackPanel>

                        <ListBox x:Name="ListBoxLetter" ItemsSource="{Binding CourriersListe}" SelectedItem="{Binding Selection, Mode=TwoWay}" >

                          <Interactivity:Interaction.Triggers>
                            <Interactivity:EventTrigger EventName="SelectionChanged" >
                              <Command:EventToCommand Command="{Binding SelectionCommand}" CommandParameter="{Binding Selection}" />
                            </Interactivity:EventTrigger>
                          </Interactivity:Interaction.Triggers>

                            <ListBox.ItemTemplate>
                                <DataTemplate>
                                    <StackPanel>
                                        <StackPanel Orientation="Vertical">
                                            <StackPanel Orientation="Horizontal">
                                                <TextBlock Text="{Binding Date}" />
                                                <TextBlock Text="{Binding Name}"/>
                                            </StackPanel>
                                            <TextBlock Text="{Binding Title}" />
                                        </StackPanel>
                                    </StackPanel>
                                </DataTemplate>
                            </ListBox.ItemTemplate>
                        </ListBox>

                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox> 
    </StackPanel>`

“Courriers”是以下类的对象:

public class MyClass
{
    public DateTime Date { get; set; }

    public List<LetterInfos> CourriersListe { get; set; }
}

LetterInfos 有日期、名称和标题。

Selection 是一个 LetterInfos 对象,我想在我的列表中单击它时获取它。

我正在使用 mvvm light,所以在我的 ViewModel 构造函数中我有这个:

            SelectionCommand = new RelayCommand<LetterInfos>(OnSelectionElement);

我试图移动外部列表框中的交互段落,但我只能获取 MyClass 选定项,并且我想选择一个 LetterInfos 项.. 任何人都可以帮助我吗?谢谢!!

【问题讨论】:

    标签: c# xaml windows-phone-8 listbox


    【解决方案1】:

    经过多次研究,我认为在这种情况下是不可能得到 selectedItem 的。 但我找到了解决这个问题的好方法:LongListSelector

    即使它的 selectedItem 不可绑定,也有一个类可以使其可绑定(到命令的参数):Trouble binding LongListSelector.SelectedItem to MVVM property

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-03
      • 1970-01-01
      • 1970-01-01
      • 2011-08-23
      相关资源
      最近更新 更多