从后面的代码绑定VirtualizingStackPanel

时间:2014-07-02 10:32:31

标签: c# xaml windows-phone

我正在使用此示例

http://msdn.microsoft.com/en-us/library/system.windows.controls.virtualizingstackpanel.virtualizationmode.aspx

但是我想从后面的代码中绑定我的集合命令。所以基本上如何使用c#?

<StackPanel.Resources>
<src:LotsOfItems x:Key="data"/>
  </StackPanel.Resources>

  <ListBox Height="150" ItemsSource="{StaticResource data}" 
             VirtualizingStackPanel.VirtualizationMode="Recycling" />

1 个答案:

答案 0 :(得分:0)

你去..

XAML

<Page
    x:Class="VirtualizingStackPanelTest.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:VirtualizingStackPanelTest"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

    <Grid Height="344" Width="475">
        <Grid.RowDefinitions>
            <RowDefinition Height="50" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>

        <Button Content="Button" Height="23" HorizontalAlignment="Left"  Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
        <ListBox Grid.Row="1" Name="theList"  VirtualizingStackPanel.VirtualizationMode="Recycling" Margin="0,0,37,0" Height="255" VerticalAlignment="Top" HorizontalAlignment="Right" Width="426" />

    </Grid>
</Page>

背后的代码:

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238

namespace VirtualizingStackPanelTest
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();

            this.NavigationCacheMode = NavigationCacheMode.Required;
        }



        private void button1_Click(object sender, RoutedEventArgs e)
        {
            var items = new LotsOfItems();

            this.Resources.Add("data", items);

            theList.ItemsSource = (LotsOfItems)this.Resources["data"];

        }
    }

    public class LotsOfItems : ObservableCollection<String>
    {
        public LotsOfItems()
        {
            for (int i = 0; i < 1000; ++i)
            {
                Add("item " + i.ToString());
            }
        }
    }
}

<强>更新

对于ListBox上的Tapped事件,您可以使用以下

替换ListBox xaml代码
  <ListBox Grid.Row="1" Name="theList"  VirtualizingStackPanel.VirtualizationMode="Recycling" Margin="0,0,37,0" Height="255" VerticalAlignment="Top" HorizontalAlignment="Right" Width="426" Tapped="theList_Tapped" />

此更改的示例代码隐藏如下:

 private void theList_Tapped(object sender, TappedRoutedEventArgs e)
        {

            if(theList.SelectedIndex >= 0)
            button1.Content = theList.SelectedItem.ToString();
        }

此致 Kajal