0

我有一个包含 many 的Menu对象(设置为 DataContext)Options (Menu.Options),其中包含 aName (Option.Name)和 many Options (Option.Options)。集合都是 type ObservableCollection<T>

菜单是从 XML 文件加载的,因此选项和值的数量可能会有所不同。

为了帮助可视化,这里是相关的 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>

我有一个ListPicker_SelectionChanged关于 SelectionChanged 事件的方法,我想在其中以某种方式标记数据绑定模型中的当前选择。我只需要使用提供给方法的参数来完成它,因为每个 ListPicker 都是在运行时生成的。所以我不能指定实际的控件名称(据我所知)。

我可以看到两种可能的选择:

1)有一个CurrentSelectioninside Menu.Option,我可以在其中引用该 ListPicker 的最后一个选定项目

2)SelectedOption.Option. 但这里的缺点是确保在选择新元素时取消选择所有元素。

我已经尝试浏览 的对象树sender,但我发现的任何东西,例如ItemsHost,都是不可访问的(私有/受保护的)。

有什么办法可以解决吗?

4

1 回答 1

0

选项 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);
                }
            }
        }

    }

}
于 2012-02-21T00:23:03.277 回答