c# - Xaml ListView 以编程方式 C# 导致奇怪的行为
问题描述
我需要以编程方式添加一个动态变化的列表视图......但我遇到了一个非常奇怪的布局。我觉得我忽略了一些东西。我需要每一行分别显示射手姓名、球队名称和他们的分数。签出代码。对此进行了编辑,以使其更易于阅读和理解。即使只有一个射击者名字的标签,它也有奇怪的用户界面问题。
private Grid grid = new Grid() { BackgroundColor = Color.White };
private BoxView boxview = new BoxView() { BackgroundColor = Color.LightGray };
private Grid grid2 = new Grid() { BackgroundColor = Color.White, Margin = new Thickness(0, 0, 0, 1), Padding = new Thickness(0, 10, 0, 0) };
var shooterlabel = new Label() { HorizontalTextAlignment = TextAlignment.Start, Padding = PaddingLeft };
shooterlabel.SetBinding(Label.TextProperty, new Binding("Shooter_Name"));
grid2.Children.Add(shooterlabel, 0, 0);
//grid.Children.Add(boxview);
grid.Children.Add(grid2);
ListView ShooterListView = new ListView() { BackgroundColor = Color.White, SeparatorVisibility = SeparatorVisibility.None, HasUnevenRows = true, VerticalScrollBarVisibility = ScrollBarVisibility.Always };
var ShooterDataTemplate = new DataTemplate(() =>
{
return new ViewCell { View = grid };
});
var connect = new ConnectionQuery("Select * From Shooters");
var list = await connect.GetShooters();
ShooterListView.ItemsSource = list;
ShooterListView.ItemTemplate = ShooterDataTemplate;
ShooterGrid.Children.Add(ShooterListView);
connect.CloseConnection();
下面是正在发生的事情的屏幕截图。它应该是每一行的名称。我只在 c# 中遇到这个问题,而不是在 xaml 中。我必须让这个在代码后面工作......
在下面附加 xaml 确实适用于包含的图片。
<StackLayout x:Name="Test">
<Grid x:Name="ShooterGrid" >
<ListView x:Name="ShooterListView" >
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid>
<Label Text="{Binding Shooter_Name}" />
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
</StackLayout>
下面附上的是在 xaml 中完成时正确的 ui。
解决方案
您可以将其设置HasUnevenRows
为 true 以删除您不想要的空间。
完整代码:
public class Page1 : ContentPage
{
public static ObservableCollection<Names> names { get; set; }
public Page1()
{
ObservableCollection<Names> names = new ObservableCollection<Names>()
{
new Names(){ Shooter_Name="Brain Palmer"},
new Names(){ Shooter_Name="John Hagee"},
new Names(){ Shooter_Name="Anget Jones"},
new Names(){ Shooter_Name="Ron Kelly"},
new Names(){ Shooter_Name="Jay Knight"},
new Names(){ Shooter_Name="Jamie Redferm"},
new Names(){ Shooter_Name="John Randolf"},
new Names(){ Shooter_Name="Johnny Reddings"},
new Names(){ Shooter_Name="Johnny john"},
new Names(){ Shooter_Name="James Dun"},
new Names(){ Shooter_Name="James Green"},
new Names(){ Shooter_Name="Sunny Perdue"},
new Names(){ Shooter_Name="John Gree"},
new Names(){ Shooter_Name="Jamoe Parrott"},
};
ListView ShooterListView = new ListView() { HasUnevenRows = true };
ShooterListView.ItemsSource = names;
ShooterListView.ItemTemplate = new DataTemplate(() =>
{
Grid grid = new Grid();
Label NameLabel = new Label();
NameLabel.SetBinding(Label.TextProperty, "Shooter_Name");
grid.Children.Add(NameLabel);
return new ViewCell { View = grid };
});
//DataTemplate dt = new DataTemplate(typeof(TextCell));
//dt.SetBinding(TextCell.TextProperty, new Binding("Shooter_Name"));
//ShooterListView.ItemTemplate= dt;
Content = new StackLayout
{
Children = {
ShooterListView
}
};
this.BindingContext = this;
}
}
public class Names
{
public string Shooter_Name { get; set; }
}
前:
后:
推荐阅读
- c# - 更新数据库并使用数据表和数据适配器
- http - 密码重置 (Parse-server/Heroku) -> {"error":"unauthorized"}
- php - 即使条件为假,PHP Laravel save() 也会保存
- r - 根据 R 中的条件为数据框的单元格着色
- python-3.x - 定义一个函数来查找给定字符串中的唯一回文
- java - 尝试使用 Hibernate 进行查询时出错(在 id 列表中的位置)
- c# - .net 属性不会从 json 反序列化为 .net 对象
- php - Laravel 将变量或对象共享给 Admin 中的所有控制器
- c# - 有没有办法避免需要受保护的覆盖 async void OnStart()
- azure-devops - 有没有办法在 Aure DevOps Nuget 推送任务中将 CI 构建自动推送到 @Release?