gpt4 book ai didi

c# - 相当于 foreach 的 XAML

转载 作者:行者123 更新时间:2023-11-30 13:56:32 25 4
gpt4 key购买 nike

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

这是包含 Item 的类以及创建我希望传递给 View 的 Items 列表的方法:

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

我的 View 由一个 XAML 模板调用 GroupDetailPage 组成,它由左侧的 ListView 和中间的“详细 View ”组成,它应该根据我从列表中选择的项目进行切换。这是 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>

这是 View 的代码隐藏。我想我需要在这里创建一个方法来获取我的项目列表并将其绑定(bind)到 View ?我一直在为此苦苦挣扎。我可能会坚持我的 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")

}
}
}

有人可以在这里向我推荐正确的方向吗?我试图将我的项目列表传递给 View 并希望有一个等效的 foreach 循环或类似的东西?谢谢!

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

 var items = new ObservableCollection<Item>();
items = model.GetList();
DataContext = items;

这是否意味着我现在可以访问我的 View ?

我从链接添加了这段代码:

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

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

我能否以某种方式将我的属性绑定(bind)到中间的 textbloxk?似乎没有任何智能感知。谢谢。

最佳答案

Foreach 是通过ListBoxListView 等 ListView 实现的。最简单的形式是ItemsControl。类(无滚动等)。使用 ItemsSource 属性将该列表传递给 View 。重复代码块通常定义在ItemTemplate模板中。

看看这个答案: https://stackoverflow.com/a/3063208/876814

关于c# - 相当于 foreach 的 XAML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26998541/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com