我最近开始使用C#,遇到以下问题:我有一个名为“ Classe”的类,它为我提供了一个对象:Classe(String name)和另一个名为LesClasses的对象,它实际上是一个ObservableCollection。这样,我想在UI的GridView中添加所有“ Classe”元素。
主要的C#代码:
namespace TestProject
{
public sealed partial class MainPage : Windows.UI.Xaml.Controls.Page
{
public MainPage()
{
this.InitializeComponent();
LesClasses listeClasse = new LesClasses();
Classe maClasse = new Classe("Test");
listeClasse.Add(maClasse);
}
private void cd_Click(object sender, RoutedEventArgs e)
{
Classe classe = new Classe("test2");
LesClasses listeClasse = new LesClasses();
listeClasse.Add(classe);
}
}
public class Classe
{
public String Nom { get; set; }
public Classe(String nom)
{
this.Nom = nom;
}
}
public class LesClasses : ObservableCollection<Classe>
{
public LesClasses()
{
}
}
public class DataTemplate : FrameworkTemplate{}
}
还有XAML:
<Page
x:Class="TestProject.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:src="using:TestProject"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid>
<Grid.Resources>
<src:LesClasses x:Key="classes"/>
</Grid.Resources>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="3*"/>
<RowDefinition Height="0.5*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="20*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<TextBlock Text="MyClassroom" Grid.Column="1" Grid.Row="0" HorizontalAlignment="Center" VerticalAlignment="Center" FontFamily="Segoe UI" FontWeight="SemiLight" FontSize="46"/>
<GridView ItemsSource="{StaticResource classes}" Margin="10,10,10,10" Grid.Column="1" Grid.Row="1" HorizontalAlignment="Center" VerticalAlignment="Center" FlowDirection="LeftToRight" Height="Auto" Width="Auto">
<GridView.ItemTemplate>
<DataTemplate>
<Button Margin="10,10,10,10" Height="Auto" Width="Auto" HorizontalAlignment="Center" VerticalAlignment="Center">
<StackPanel VerticalAlignment="Center" HorizontalAlignment="Center" Orientation="Horizontal">
<SymbolIcon Symbol="OtherUser" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="10,10,10,10"/>
<StackPanel>
<TextBlock Text="{Binding Nom}" FontFamily="Segoe UI" FontWeight="SemiLight" FontSize="26"/>
</StackPanel>
</StackPanel>
</Button>
</DataTemplate>
</GridView.ItemTemplate>
<GridView.ItemsPanel>
<ItemsPanelTemplate>
<ItemsWrapGrid MaximumRowsOrColumns="3"/>
</ItemsPanelTemplate>
</GridView.ItemsPanel>
<GridView.GroupStyle>
<GroupStyle HidesIfEmpty="True">
<GroupStyle.HeaderTemplate>
<DataTemplate>
<Grid Background="LightGray" Margin="0">
<TextBlock Text='{Binding Name}'
Foreground="Black" Margin="30"
Style="{StaticResource HeaderTextBlockStyle}"/>
</Grid>
</DataTemplate>
</GroupStyle.HeaderTemplate>
</GroupStyle>
</GridView.GroupStyle>
</GridView>
<Button Name="cd" Click="cd_Click" Grid.Column="1" Grid.Row="2" Content="Ajouter la classe test2" HorizontalAlignment="Stretch" VerticalAlignment="Bottom" FontFamily="Segoe UI" FontSize="18" FontWeight="SemiLight"/>
</Grid>
</Page>
当我启动该应用程序并单击该按钮时,没有任何反应。有人可以帮我吗?
答案 0 :(得分:1)
以最简单的方式,这是解决问题的方法,在代码中,您需要执行以下操作:
为可观察的集合添加属性:
rake db:migrate
初始化列表:
public LesClasses Classes { get; set; }
将MainPage.cs分配为您的DataContext
Classes = new LesClasses();
Classe maClasse = new Classe("Test");
Classes.Add(maClasse);
在这种情况下,您需要将新的Classe实例添加到可观察的集合属性中。
DataContext = this;
在您的xaml中,更改ItemSource以绑定到您的collection属性:
private void cd_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
{
Classe classe = new Classe("test2");
Classes.Add(classe);
}
您的MainPage类应如下所示:
<GridView ItemsSource="{Binding Classes}"
输出:
继续学习C#和UWP / WPF。