问题描述
我有一个项目。我有一张桌子。 (由网格制成)我有一个条目/文本框。我需要让客户将数字写入条目(让我们称该数字为“n”)。然后我需要在由网格制成的表格内添加 n 行。我该怎么做?
这是我制作网格表的代码。
gr.RowDeFinitions.Add(new RowDeFinition
{
Height = new GridLength(1,GridUnitType.Absolute)
});
gr.RowDeFinitions.Add(new RowDeFinition
{
Height = new GridLength(1,GridUnitType.Star)
});
gr.RowDeFinitions.Add(new RowDeFinition
{
Height = new GridLength(1,GridUnitType.Absolute)
});
gr.ColumnDeFinitions.Add(new ColumnDeFinition
{
Width = new GridLength(1,GridUnitType.Absolute)
});
gr.ColumnDeFinitions.Add(new ColumnDeFinition
{
Width = new GridLength(1,GridUnitType.Star)
});
gr.ColumnDeFinitions.Add(new ColumnDeFinition
{
Width = new GridLength(1,GridUnitType.Absolute)
});
var backgroundBox = new BoxView
{
Color = System.Drawing.Color.FromArgb(-32513)
};
gr.Children.Add(backgroundBox,1);
Grid.SetColumnSpan(backgroundBox,5);
var ustyatay = new BoxView { Color = System.Drawing.Color.Gray };
gr.Children.Add(ustyatay,0);
Grid.SetColumnSpan(ustyatay,5);
var yatay2 = new BoxView { Color = System.Drawing.Color.Gray };
gr.Children.Add(yatay2,2);
Grid.SetColumnSpan(yatay2,5);
var yatay3 = new BoxView { Color = Xamarin.Forms.Color.Gray };
gr.Children.Add(yatay3,4);
Grid.SetColumnSpan(yatay3,5);
var yatay4 = new BoxView { Color = Xamarin.Forms.Color.Gray };
gr.Children.Add(yatay4,6);
Grid.SetColumnSpan(yatay4,5);
var soldik = new BoxView { Color = System.Drawing.Color.Gray };
gr.Children.Add(soldik,0);
Grid.SetRowSpan(soldik,7);
var ortadik = new BoxView { Color = Xamarin.Forms.Color.Gray };
gr.Children.Add(ortadik,2,0);
Grid.SetRowSpan(ortadik,7);
var sagdik = new BoxView { Color = System.Drawing.Color.Gray };
gr.Children.Add(sagdik,4,0);
Grid.SetRowSpan(sagdik,7);
gr.Children.Add(new Label
{
Text = "Customer Name",FontAttributes = FontAttributes.Bold,TextColor = System.Drawing.Color.Yellow,FontSize = 16,Padding=new Thickness(10,10)
},1,1); ;
gr.Children.Add(new Label
{
Text = "T.Type Name",TextColor= Xamarin.Forms.Color.Yellow,FontSize=16,Padding = new Thickness(10,10)
},3,1);
我也将线条作为网格列和行。我想我做错了。当我添加 n 行时,我也需要更改 rowspan。我怎么能做那个项目。你们能帮我吗?我需要学习:如何添加带有条目的行,如何为新行(用于生成行)添加框视图和行跨度?谢谢大家的帮助!
那张我的手绘应该怎么做的照片:https://prnt.sc/10jxdhn
解决方法
我可以通过使用数据绑定来解决这个问题。 首先,像这样编辑您的 .xaml 文件:
<Entry Text="{Binding N}"></Entry>
<Button Text="Create" Command ="{Binding CreateCommand}"></Button>
<ListView ItemsSource="{Binding Rows}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto"/>
</Grid.ColumnDefinitions>
<Label Text="{Binding}"></Label>
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
接下来创建一个 ViewModel,我称之为 Page1ViewModel.cs。 在.xaml.cs文件中,将绑定上下文添加到构造函数中的Page1ViewModel
BindingContext = new Page1ViewModel();
在 Page1ViewModel.cs 中:
class Page1ViewModel : INotifyPropertyChanged
{
private int n;
private List<string> rows;
public List<string> Rows
{
get => rows;
set
{
rows = value;
OnPropertyChanged();
}
}
public int N
{
get => n;
set
{
n = value;
OnPropertyChanged();
}
}
public Command CreateCommand { get; }
public Page1ViewModel()
{
Rows = new List<string>();
CreateCommand = new Command(Create);
}
private void Create()
{
List <string> tmp = new List<string>();
for (int i = 0; i < n; i++)
{
tmp.Add("Row" + i);
}
Rows = tmp;
}
#region INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string propertyName = "")
{
var changed = PropertyChanged;
if (changed == null)
return;
changed.Invoke(this,new PropertyChangedEventArgs(propertyName));
}
#endregion
}
祝你好运!