WP7 ListPicker - How to mark selected item in model when generated from DataSource?
-
28-10-2019 - |
Question
I have a Menu
object (set as the DataContext) which contains many Options (Menu.Options)
, which contains a Name (Option.Name)
and many Options (Option.Options)
. The collections are all of type ObservableCollection<T>
.
The Menu is loaded from an XML file, so the amount of Options and Values can vary.
To help visualise, here is the relevant XAML:
<ListBox ItemsSource="{Binding Path=Options}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<!-- Title -->
<TextBlock Text="{Binding Path=Name}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}" />
<!-- Selection -->
<toolkit:ListPicker ItemsSource="{Binding Path=Options}" SelectionChanged="ListPicker_SelectionChanged">
<toolkit:ListPicker.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Name}" />
</DataTemplate>
</toolkit:ListPicker.ItemTemplate>
<toolkit:ListPicker.FullModeItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Name}" />
</DataTemplate>
</toolkit:ListPicker.FullModeItemTemplate>
</toolkit:ListPicker>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
I have a method ListPicker_SelectionChanged
on the SelectionChanged event, in which I want to somehow mark the current selection in the databound model. I need to do it using just the parameters supplied to the method, as each ListPicker is generated at runtime. So I can't be specifying actual control names (as far as I'm aware anyway).
I can see two possible options:
1) To have a CurrentSelection
inside Menu.Option
, where I can put a reference to the last selected item for that ListPicker
2) To have a Selected
attribute on the Option.Option
. Downside here though is making sure all elements are deselected when a new one is selected.
I've tried browsing the object tree of sender
, but anything I find, such as ItemsHost
, is inaccessible (private/protected).
Is there any way I can achieve a solution?
Solution
Option 1 is the better one, use TwoWay binding on the ListPicker.SelectedItem.
<phone:PhoneApplicationPage
x:Class="StackOverflowWP7.SO9369491"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit"
xmlns:local="clr-namespace:StackOverflowWP7.SO9369491_"
mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
shell:SystemTray.IsVisible="True">
<phone:PhoneApplicationPage.DataContext>
<local:Menu>
<local:Menu.Options>
<local:Option Description="Size">
<local:Option.Options>
<local:Option local:Description="Large" />
<local:Option local:Description="Regular" />
</local:Option.Options>
<local:Option.CurrentSelection>
<local:Option local:Description="Regular" />
</local:Option.CurrentSelection>
</local:Option>
</local:Menu.Options>
</local:Menu>
</phone:PhoneApplicationPage.DataContext>
<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<!--TitlePanel contains the name of the application and page title-->
<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
<TextBlock x:Name="ApplicationTitle" Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}"/>
<TextBlock x:Name="PageTitle" Text="page name" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
</StackPanel>
<!--ContentPanel - place additional content here-->
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<ListBox ItemsSource="{Binding Path=Options}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<!-- Title -->
<TextBlock Text="{Binding Path=Description}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}" />
<!-- Selection -->
<toolkit:ListPicker ItemsSource="{Binding Path=Options}"
SelectedItem="{Binding CurrentSelection, Mode=TwoWay}" >
<toolkit:ListPicker.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Description}" />
</DataTemplate>
</toolkit:ListPicker.ItemTemplate>
<toolkit:ListPicker.FullModeItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Description}" />
</DataTemplate>
</toolkit:ListPicker.FullModeItemTemplate>
</toolkit:ListPicker>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</Grid>
</phone:PhoneApplicationPage>
Here's the Code Behind
namespace StackOverflowWP7
{
using Microsoft.Phone.Controls;
public partial class SO9369491 : PhoneApplicationPage
{
// Constructor
public SO9369491()
{
InitializeComponent();
}
}
}
namespace StackOverflowWP7.SO9369491_
{
using System.Collections.ObjectModel;
using System.Linq;
public class Menu : Option
{
public Menu()
: base("Main Menu")
{
}
}
public class Option
{
public Option() {}
public Option(string Name, params string[] choices)
{
this.Description = Name;
foreach (var choice in choices)
{
this._options.Add(new Option(choice));
}
}
public string Description { get; set; }
private ObservableCollection<Option> _options = new ObservableCollection<Option>();
public ObservableCollection<Option> Options { get { return _options; } }
private Option _CurrentSelection;
public Option CurrentSelection
{
get {
return _CurrentSelection;
}
set
{
if (_options.Contains(value))
{
_CurrentSelection = value;
}
else
{
_CurrentSelection = _options.FirstOrDefault((o) => o.Description == value.Description);
}
}
}
}
}