How to support ListBox SelectedItems binding with MVVM in a navigable application

前端 未结 9 1180
有刺的猬
有刺的猬 2020-11-27 15:06

I am making a WPF application that is navigable via custom \"Next\" and \"Back\" buttons and commands (i.e. not using a NavigationWindow). On one screen, I have

相关标签:
9条回答
  • 2020-11-27 16:01

    Turns out binding a check box to the IsSelected property and putting the textblock and checkbox within a stack panel does the trick!

    0 讨论(0)
  • 2020-11-27 16:03

    I kept looking into an easy solution for this but with no luck.

    The solution Rachel has is good if you already have the Selected property on the object within your ItemsSource. If you do not, you have to create a Model for that business model.

    I went a different route. A quick one, but not perfect.

    On your ListBox create an event for SelectionChanged.

    <ListBox ItemsSource="{Binding SomeItemsSource}"
             SelectionMode="Multiple"
             SelectionChanged="lstBox_OnSelectionChanged" />
    

    Now implement the event on the code behind of your XAML page.

    private void lstBox_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        var listSelectedItems = ((ListBox) sender).SelectedItems;
        ViewModel.YourListThatNeedsBinding = listSelectedItems.Cast<ObjectType>().ToList();
    }
    

    Tada. Done.

    This was done with the help of converting SelectedItemCollection to a List.

    0 讨论(0)
  • 2020-11-27 16:06

    Try creating an IsSelected property on each of your data items and binding ListBoxItem.IsSelected to that property

    <Style TargetType="{x:Type ListBoxItem}">
        <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
    </Style>
    
    0 讨论(0)
提交回复
热议问题