ListBox绑定上的选定项目?

时间:2011-11-16 03:50:57

标签: c# silverlight windows-phone-7

我有一个ListBox的绑定,但我如何添加一个事件到选定的项目?

MainPage.xaml中:

<!--ContentPanel - place additional content here-->
<StackPanel x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
    <ListBox Name="list" SelectionChanged="list_SelectionChanged">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <Image Source="{Binding ImageUri}" 
                           Stretch="None" 
                           Height="100"/>
                    <StackPanel Width="360" >
                        <TextBlock Text="{Binding Text}"
                                   Style="{StaticResource PhoneTextExtraLargeStyle}"/>
                        <TextBlock Text="{Binding Opis}" 
                                   Style="{StaticResource PhoneTextSubtleStyle}"/>
                    </StackPanel>
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</StackPanel>

MainPage.xaml.cs中:

public partial class MainPage : PhoneApplicationPage
{
    public MainPage()
    {
        InitializeComponent();
        ObservableCollection<SampleData> dataSource = 
            new ObservableCollection<SampleData>();

        dataSource.Add(new SampleData() 
        { 
            ImageUri = "Images/appbar.delete.rest.png", 
            Text = "Item1", 
            Description = "blablabla" 
        });
        dataSource.Add(new SampleData() 
        { 
            ImageUri = "Images/appbar.delete.rest.png", 
            Text = "Item2", 
            Description = "blablabla" 
        });
        dataSource.Add(new SampleData()
        { 
            ImageUri = "Images/appbar.download.rest.png", 
            Text = "Item3", 
            Description = "blablabla" 
        });

        this.list.ItemsSource = dataSource;         
    }

    private void list_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if (list.SelectedItem == null) return;
        //what next?
    }

    public class SampleData
    {
        public string Text { get; set; }
        public string Description { get; set; }
        public string ImageUri { get; set; }
    }
}

我想点击Item2上的例子,然后转到Page2.xaml。

页面

来自VS2010的我的项目:http://www.przeklej.pl/plik/wp7sampleproject6-7z-00368v7i196u

3 个答案:

答案 0 :(得分:1)

如果您正在使用像MVVMlight这样的MVVM堆栈,这很简单,因为您只需在视图模型中为选定项创建一个完整属性,然后像其他任何东西一样绑定到它。例如,使用上面的代码,我会这样做:

        /// <summary>
    /// The <see cref="SelectedListItem" /> property's name.
    /// </summary>
    public const string SelectedStickPropertyName = "SelectedListItem";

    private SampleData _selectedItem;

    /// <summary>
    /// Gets the SelectedStick property.
    /// TODO Update documentation:
    /// Changes to that property's value raise the PropertyChanged event. 
    /// This property's value is broadcasted by the Messenger's default instance when it changes.
    /// </summary>
    public SampleData SelectedListItem
    {
        get
        {
            return _selectedItem;
        }

        set
        {
            if (_selectedItem == value || value == null)
            {
                return;
            }

            var oldValue = _selectedItem;
            _selectedItem = value;

            // Update bindings, no broadcast
            RaisePropertyChanged(SelectedStickPropertyName);
        }
    }

然后在您的Listbox XAML中,只需将Listbox Selected Item绑定到上面的属性(前提是您已设置了数据上下文属性:

<ListBox Name="list" SelectionChanged="list_SelectionChanged" SelectedItem="{Binding SelectedListItem, Mode=TwoWay}">

通过这种方式,您可以查询Selected Item属性以查找值。

答案 1 :(得分:0)

我个人会使用带有Command绑定的Buttons,但我想你可以这样开始:

public MainPage()
{
    InitializeComponent();
    ObservableCollection<SampleData> dataSource = new ObservableCollection<SampleData>();

    dataSource.Add(
        new SampleData()
        {
            ImageUri = "Images/appbar.delete.rest.png",
            Text = "Item1",
            Description = "blablabla",
            TargetUri = new Uri("Page1.xaml", UriKind.Relative)
        });
    dataSource.Add(
        new SampleData()
        {
            ImageUri = "Images/appbar.delete.rest.png",
            Text = "item2",
            Description = "blablabla",
            TargetUri = new Uri("Page2.xaml", UriKind.Relative)
        });
    dataSource.Add(
        new SampleData()
        {
            ImageUri = "Images/appbar.download.rest.png",
            Text = "Item3",
            Description = "blablabla",
            TargetUri = new Uri("Page3.xaml", UriKind.Relative)
        });

    this.list.ItemsSource = dataSource;
}

private void list_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    var data = list.SelectedItem as SampleData;

    if (data == null)
    {
        return;
    }

    NavigationService.Navigate(data.TargetUri);
}

public class SampleData
{
    public string Text { get; set; }

    public string Description { get; set; }

    public string ImageUri { get; set; }
    public Uri TargetUri { get; set; }
}

*编辑

如果你想点击一个项目来使用命令进行导航,你可以这样做:

public class SampleData
{
    public string Text { get; set; }

    public string Description { get; set; }

    public string ImageUri { get; set; }
    public Uri TargetUri { get; set; }
    public ICommand NavigateCommand { get; private set; }

    public SampleData()
    {
        NavigateCommand =
            new DelegateCommand(
                () => NavigationService.Navigate(TargetUri);
    }
}

要获取NavigationService实例 - 您可以自己实现它,或者只是使用App.RootFrame来执行导航调用。您可以大致如下定义命令绑定:

<ListBox.ItemTemplate>
    <DataTemplate>
        <Button
            Command={Binding NavigateCommand}>
            <Button.Template>
                <ControlTemplate>
                    <StackPanel Orientation="Horizontal">
                        <Image Source="{Binding ImageUri}" 
                               Stretch="None" 
                               Height="100"/>
                        <StackPanel Width="360" >
                            <TextBlock Text="{Binding Text}"
                                       Style="{StaticResource PhoneTextExtraLargeStyle}"/>
                            <TextBlock Text="{Binding Description}" 
                                       Style="{StaticResource PhoneTextSubtleStyle}"/>
                        </StackPanel>
                </StackPanel>
                </ControlTemplate>
            </Button.Template>
        </Button>
    </DataTemplate>
</ListBox.ItemTemplate>

答案 2 :(得分:0)

查看默认情况下创建的代码,作为新数据绑定应用程序的一部分。它显示了如何通过查询SelectionChanged事件处理程序中的e.AddedItems属性来访问SelectedItem。