简体   繁体   中英

How to get selected item of a listbox within a listbox

I have a listbox in the dataTemplate of an other listbox. I can get selected item of the outer listbox, but I've been trying to get selected item of the inner listbox (name: "ListBoxLetter"), no way ..

Here is my 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" is an object of the following class :

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

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

And a LetterInfos has a Date, Name and Title.

Selection is a LetterInfos object, and I want to get it when I click on it in my list.

I'm working with mvvm light, so in my ViewModel constructor I have this :

            SelectionCommand = new RelayCommand<LetterInfos>(OnSelectionElement);

I tried to move the interactivity paragraph in the outer listbox, but I can only get the MyClass selected item, and I want to select a LetterInfos item.. Anyone could help me ? Thanks !!

After many researches, I think that's not possible to get selectedItem in this situation. But I found a good solution to this problem : the LongListSelector .

Even if its selectedItem is not bindable, there is a class to add that can make it bindable (to the parameter of a command) : Trouble binding LongListSelector.SelectedItem to MVVM property

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM