C# foreach的XAML等价物

C# foreach的XAML等价物,c#,xaml,C#,Xaml,好的,所以我尝试将项目列表传递到XAML视图中,以便“循环”它们。我习惯于使用html,但还没有真正理解如何在XAML中实现这一点 下面是一个包含项的类,也是一个创建要传递给视图的项列表的方法: public class Item { public int ItemId { get; set; } public string Name { get; set; } public int Age { get; set; } pu

好的,所以我尝试将项目列表传递到XAML视图中,以便“循环”它们。我习惯于使用html,但还没有真正理解如何在XAML中实现这一点

下面是一个包含项的类,也是一个创建要传递给视图的项列表的方法:

public class Item
    {
        public int ItemId { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }
        public string Description { get; set; }



        public List<Item> GetList()
        {

            var _test = new List<Item>();
            for (int i = 0; i < 10; i++)
            {
                var newItem = new Item()
                {
                    ItemId = i++,
                    Name = "Person",
                    Age = 30,
                    Description = "Description",

                };

                _test.Add(newItem);
            }

            return _test;
        } 
    }
公共类项目
{
公共int ItemId{get;set;}
公共字符串名称{get;set;}
公共整数{get;set;}
公共字符串说明{get;set;}
公共列表GetList()
{
var_test=新列表();
对于(int i=0;i<10;i++)
{
var newItem=newItem()
{
ItemId=i++,
Name=“Person”,
年龄=30岁,
Description=“Description”,
};
_测试。添加(新项);
}
回归检验;
} 
}

我的视图由一个XAML模板Cales GubDebug页面组成,该页面由左视图ListVIEW和中间的“详细视图”组成,它应该根据我选择的列表中的项目来切换。以下是列表视图的XAML,未触及:

 <ListView
            x:Name="itemListView"
            AutomationProperties.AutomationId="ItemsListView"
            AutomationProperties.Name="Items"
            TabIndex="1"
            Grid.Row="1"
            Margin="-10,-10,0,0"
            Padding="120,0,0,60"
            ItemsSource="{Binding Source={StaticResource itemsViewSource}}"
            IsSwipeEnabled="False"
            SelectionChanged="ItemListView_SelectionChanged">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <Grid Margin="6">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto"/>
                            <ColumnDefinition Width="*"/>
                        </Grid.ColumnDefinitions>
                        <Border Background="{ThemeResource ListViewItemPlaceholderBackgroundThemeBrush}" Width="60" Height="60">
                            <Image Source="{Binding ImagePath}" Stretch="UniformToFill" AutomationProperties.Name="{Binding Title}"/>
                        </Border>
                        <StackPanel Grid.Column="1" Margin="10,0,0,0">
                            <TextBlock Text="{Binding Title}" Style="{StaticResource TitleTextBlockStyle}" TextWrapping="NoWrap" MaxHeight="40"/>
                            <TextBlock Text="{Binding Subtitle}" Style="{StaticResource CaptionTextBlockStyle}" TextWrapping="NoWrap"/>
                        </StackPanel>
                    </Grid>
                </DataTemplate>
            </ListView.ItemTemplate>
            <ListView.ItemContainerStyle>
                <Style TargetType="FrameworkElement">
                    <Setter Property="Margin" Value="0,0,0,10"/>
                </Style>
            </ListView.ItemContainerStyle>
        </ListView>

下面是视图的代码。我想我需要在这里创建一个方法来获取我的项目列表并将其绑定到视图?一、 我一直在为此奋斗。我可能陷入了我的MVC思维中,想知道如何去做:

  public sealed partial class GroupDetailPage1 : Page
    {
        private NavigationHelper navigationHelper;
        private ObservableDictionary defaultViewModel = new ObservableDictionary();

        /// <summary>
        /// This can be changed to a strongly typed view model.
        /// </summary>
        public ObservableDictionary DefaultViewModel
        {
            get { return this.defaultViewModel; }
        }

        /// <summary>
        /// NavigationHelper is used on each page to aid in navigation and 
        /// process lifetime management
        /// </summary>
        public NavigationHelper NavigationHelper
        {
            get { return this.navigationHelper; }
        }

        public GroupDetailPage1()
        {
            this.InitializeComponent();

            // Setup the navigation helper
            this.navigationHelper = new NavigationHelper(this);
            this.navigationHelper.LoadState += navigationHelper_LoadState;
            this.navigationHelper.SaveState += navigationHelper_SaveState;

            // Setup the logical page navigation components that allow
            // the page to only show one pane at a time.
            this.navigationHelper.GoBackCommand = new SimpleMapping.Common.RelayCommand(() => this.GoBack(), () => this.CanGoBack());
            this.itemListView.SelectionChanged += itemListView_SelectionChanged;

            // Start listening for Window size changes 
            // to change from showing two panes to showing a single pane
            Window.Current.SizeChanged += Window_SizeChanged;
            this.InvalidateVisualState();
        }

        void itemListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (this.UsingLogicalPageNavigation())
            {
                this.navigationHelper.GoBackCommand.RaiseCanExecuteChanged();
            }
        }


        private void navigationHelper_LoadState(object sender, LoadStateEventArgs e)
        {
            // TODO: Assign a bindable group to Me.DefaultViewModel("Group")
            // TODO: Assign a collection of bindable items to Me.DefaultViewModel("Items")

            if (e.PageState == null)
            {
                // When this is a new page, select the first item automatically unless logical page
                // navigation is being used (see the logical page navigation #region below.)
                if (!this.UsingLogicalPageNavigation() && this.itemsViewSource.View != null)
                {
                    this.itemsViewSource.View.MoveCurrentToFirst();
                }
            }
            else
            {
                // Restore the previously saved state associated with this page
                if (e.PageState.ContainsKey("SelectedItem") && this.itemsViewSource.View != null)
                {
                    // TODO: Invoke Me.itemsViewSource.View.MoveCurrentTo() with the selected
                    //       item as specified by the value of pageState("SelectedItem")

                }
            }
        }
公共密封部分类GroupDetailPage1:第页
{
私人导航助手导航助手;
private observedictionary defaultViewModel=new observedictionary();
/// 
///可以将其更改为强类型视图模型。
/// 
公共可观测默认视图模型
{
获取{返回this.defaultViewModel;}
}
/// 
///NavigationHelper在每个页面上用于帮助导航和
///过程生命周期管理
/// 
公共导航帮助器导航帮助器
{
获取{返回this.navigationHelper;}
}
公共组详细信息第1页()
{
this.InitializeComponent();
//设置导航助手
this.navigationHelper=新的navigationHelper(this);
this.navigationHelper.LoadState+=navigationHelper\u LoadState;
this.navigationHelper.SaveState+=navigationHelper\u SaveState;
//设置允许以下操作的逻辑页面导航组件
//该页面一次只能显示一个窗格。
this.navigationHelper.GoBackCommand=new SimpleMapping.Common.RelayCommand(()=>this.GoBack(),()=>this.CanGoBack());
this.itemListView.SelectionChanged+=itemListView\u SelectionChanged;
//开始侦听窗口大小的更改
//从显示两个窗格更改为显示单个窗格的步骤
Window.Current.SizeChanged+=Window\u SizeChanged;
此.InvalidateVisualState();
}
void itemListView\u SelectionChanged(对象发送者,selectionchangedventargs e)
{
if(此.usingLogicalAgenavigation())
{
this.navigationHelper.GoBackCommand.RaiseCanExecuteChanged();
}
}
私有void navigationHelper_LoadState(对象发送方,LoadStateEventArgs e)
{
//TODO:将可绑定组分配给我。DefaultViewModel(“组”)
//TODO:将可绑定项的集合分配给我。DefaultViewModel(“项”)
if(e.PageState==null)
{
//当这是一个新页面时,自动选择第一项,除非是逻辑页面
//正在使用导航(请参阅下面的逻辑页面导航#区域。)
如果(!this.usingLogicalAgenavigation()&&this.itemsViewSource.View!=null)
{
this.itemsViewSource.View.MoveCurrentToFirst();
}
}
其他的
{
//还原与此页关联的以前保存的状态
if(e.PageState.ContainsKey(“SelectedItem”)&&this.itemsViewSource.View!=null)
{
//TODO:使用选定的
//由pageState(“SelectedItem”)值指定的项
}
}
}
有人能告诉我正确的方向吗?我正试图将我的项目列表传递给视图,并希望有一个与foreach循环或类似的等价物? 谢谢大家!

编辑: 在GroupDetailPage的构造函数中,我添加了以下代码:

 var items = new ObservableCollection<Item>();
        items = model.GetList();
        DataContext = items;
var items=新的ObservableCollection();
items=model.GetList();
DataContext=项目;
这是否意味着我可以在视图中访问

我从链接中添加了以下代码片段:

 <ItemsControl ItemsSource="{Binding}">
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <StackPanel>
                            <TextBlock Text="{Binding }"></TextBlock>

                        </StackPanel>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>


我可以把我的属性绑定到TeXBuxxk中间吗?似乎没有任何智能。谢谢。

< P>是用列表视图来实现的,如“代码> ListBox < /C>”、“代码> ListVIEW < /Cube >等。最简单的形式是类(无SCR)。