59768

How to bind a ListView with SelectionMode multiple to achieve MVVM using Behaviors SDK in Windows 8.

Question:

I have a Windows 8.1 application with a ListView with SelectionMode multiple(making the ListView allow the user to select multiple entries)

I am trying to use <a href="http://msdn.microsoft.com/en-us/library/dn457340.aspx" rel="nofollow">Behaviors SDK</a> from Microsoft

<DataTemplate x:Key="DataItemTemplate"> <TextBlock Text="{Binding Name}" Margin="10,0,0,0"/> </DataTemplate> <ListView ItemsSource="{Binding Data}" SelectionMode="Multiple" ItemTemplate="{StaticResource DataItemTemplate}"> <Interactivity:Interaction.Behaviors> <Core:EventTriggerBehavior SourceObject="{Binding}" EventName="SelectionChanged"/> </Interactivity:Interaction.Behaviors> </ListView>

As you can see the ItemSource of the ListView has been bound to the property Data which is a List in my ViewModel as shown below

public List<MyData> Data { get; set; } class MyData { public string Name { get; set; } public bool IsSelected { get; set; } }

The approach that I could think of was to set the IsSelected property to true in the class MyData if the corresponding element has been selected in my View. But that solution looks like I need to have codebehind.

I have attached the EventTriggerBehavior for my ListView but I am unable to figure out how to Bind it and what to bind it to.

I would be very glad if someone can point me in the right direction to achieve this so that my List Data will have the data for me to differentiate between my SelectedItems and non-selected items. Or better still if I can completely eliminate the IsSelected bool property it would be great.

Thanks in Advance.

Answer1:

Check out <a href="https://winrtxamltoolkit.codeplex.com/SourceControl/latest#WinRTXamlToolkit/WinRTXamlToolkit.Shared/Controls/Extensions/ListViewExtensions.cs" rel="nofollow">BindableSelection</a> in <a href="http://www.nuget.org/packages/winrtxamltoolkit" rel="nofollow">WinRT XAML Toolkit</a>.

You can use it the following way in your XAML:

xmlns:Extensions="using:WinRTXamlToolkit.Controls.Extensions" Extensions:ListViewExtensions.BindableSelection="{Binding SelectedItems, Mode=TwoWay}"

Recommend

  • Behaviors in UWP (Event trigger command error
  • Silverlight Image Flip Animation
  • Why is the brush preventing dc.js barChart toolTips to appear?
  • The name 'InitializeComponent' does not exist in the current context in WPF application
  • No drop with basic gongsolution sample
  • Datagrid scroll bar stops working
  • text along curvature path like circular or arc in silverlight
  • How to access control in DataGridTemplateColumn to get value?
  • Drag-and-Drop UWP vs Button Style
  • Silverlight UI not unsubscribing from PropertyChanged events
  • WPF DataGrid shows dot instead comma
  • Interactive Design-Time User Control
  • Deserializing nested JSON C# and Bind to XAML ListBox
  • How can I bind DataGrid row count after a CollectionView filter is applied?
  • Bind customObject to DataGrid
  • Bind events to Item ViewModel
  • Scroller-like animation on a Label in WPF
  • How to change EasingDoubleKeyFrame value at Runtime?
  • Using ListPicker and DataBinding
  • Flyout.showAt() ArgumentException based on Content
  • DataGrid: Help accessing a control defined in a CellEditingTemplate
  • How to manage state in JAX-RS?
  • Why is wpf UpdateSourceTrigger not binding explicitly?
  • How to get instance of the GridControl in LookupEdit (Devexpress)
  • How to deal with concurrency issues brought by NSStream run loop scheduling using GCD?
  • JQuery Mobile 1.4 How to Disable Hover Effect on Mobile Devices
  • Return list instead of an item Webservice Phone 8.1 UAP
  • Convert adjacency matrix to a csv file
  • How to use a MultiBinding on DataGridTextColumn?
  • Add items to ComboBox at runtime?
  • Binding ContextMenu Tag to Owner
  • Create File Command in Batch Files (*.bat)
  • Different Datacontext for Command and CommandParameter
  • Installing apk from within application in android
  • Return null in boolean to checkbox state converter in XAML
  • Angular2 component view does not update on value change via method
  • CakePHP ACL tutorial initDB function warnings
  • Listbox within Listbox and scrolling trouble in Windows Phone 7 Silverlight
  • SetUp method failed while running tests from teamcity
  • Understanding cpu registers