如何在列表框中获取列表框的选定项
How to get selected item of a listbox within a listbox
我在另一个列表框的数据模板中有一个列表框。我可以获得外部列表框的 selected 项目,但我一直试图获得内部列表框的 selected 项目(名称:"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" 是以下 class 的 object :
public class MyClass
{
public DateTime Date { get; set; }
public List<LetterInfos> CourriersListe { get; set; }
}
LetterInfos 有日期、姓名和标题。
Selection 是一个 LetterInfos object,我想在我的列表中点击它时得到它。
我正在使用 mvvm light,所以在我的 ViewModel 构造函数中我有这个:
SelectionCommand = new RelayCommand<LetterInfos>(OnSelectionElement);
我试图移动外部列表框中的交互段落,但我只能得到 MyClass selected 项,我想 select 一个 LetterInfos 项..
任何人都可以帮助我吗?谢谢!!
经过多次研究,我认为在这种情况下是不可能获得selectedItem的。
但是我找到了一个很好的解决这个问题的方法:LongListSelector
。
即使它的 selectedItem 不可绑定,也可以添加一个 class 使其可绑定(到命令的参数):Trouble binding LongListSelector.SelectedItem to MVVM property
我在另一个列表框的数据模板中有一个列表框。我可以获得外部列表框的 selected 项目,但我一直试图获得内部列表框的 selected 项目(名称:"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" 是以下 class 的 object :
public class MyClass
{
public DateTime Date { get; set; }
public List<LetterInfos> CourriersListe { get; set; }
}
LetterInfos 有日期、姓名和标题。
Selection 是一个 LetterInfos object,我想在我的列表中点击它时得到它。
我正在使用 mvvm light,所以在我的 ViewModel 构造函数中我有这个:
SelectionCommand = new RelayCommand<LetterInfos>(OnSelectionElement);
我试图移动外部列表框中的交互段落,但我只能得到 MyClass selected 项,我想 select 一个 LetterInfos 项.. 任何人都可以帮助我吗?谢谢!!
经过多次研究,我认为在这种情况下是不可能获得selectedItem的。
但是我找到了一个很好的解决这个问题的方法:LongListSelector
。
即使它的 selectedItem 不可绑定,也可以添加一个 class 使其可绑定(到命令的参数):Trouble binding LongListSelector.SelectedItem to MVVM property