选项 1 更好,在 ListPicker.SelectedItem 上使用 TwoWay 绑定。
<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>
这是背后的代码
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);
}
}
}
}
}