Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从代码隐藏绑定虚拟化StackPanel_C#_Xaml_Windows Phone - Fatal编程技术网

C# 从代码隐藏绑定虚拟化StackPanel

C# 从代码隐藏绑定虚拟化StackPanel,c#,xaml,windows-phone,C#,Xaml,Windows Phone,我用这个样品来 但是我想根据代码隐藏的命令绑定我的集合。那么基本上如何使用c来实现这一点呢 <StackPanel.Resources> <src:LotsOfItems x:Key="data"/> </StackPanel.Resources> <ListBox Height="150" ItemsSource="{StaticResource data}" VirtualizingStackPanel.Virt

我用这个样品来

但是我想根据代码隐藏的命令绑定我的集合。那么基本上如何使用c来实现这一点呢

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

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

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>
问候
Kajal

是关于Windows Phone的。这根本不起作用。这是什么:this.FindResourcedata;您好,我已更新代码以匹配windows phone实现。这是为WPF以前。FindResource。。是WPF对象成员。在windows phone中,您可以使用索引器this.Resources[数据]。上面的例子。我希望您的目标是windows phone运行时。我是,但我采取了不同的方法。仍然无法确定如何在列表框中的项目上附加点击事件。。
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 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();
        }