【问题标题】:bind VirtualizingStackPanel from code behind从后面的代码绑定 VirtualizingStackPanel
【发布时间】:2014-07-02 10:32:30
【问题描述】:

我正在使用这个示例来

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" />

【问题讨论】:

    标签: c# xaml windows-phone


    【解决方案1】:

    给你..

    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();
            }
    

    问候 卡哈尔

    【讨论】:

    • 它是关于 Windows Phone 的。这根本行不通。这是什么:this.FindResource("data");
    • 您好,我已更新代码以匹配 windows phone 实现。以前是针对 WPF 的。 FindResource(..) 是 WPF 对象成员。在 windows phone 中,您可以使用索引器 this.Resources["data"]。上面的例子。
    • 我希望你的目标是 Windows Phone 运行时。
    • 我是,但我采取了不同的方法。仍然无法弄清楚如何在 ListBox 中的项目上附加 Tap 事件..
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-05-08
    • 2014-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-21
    • 1970-01-01
    相关资源
    最近更新 更多