尝试做一个收入/月计算器。C#部分已经完成,现在我必须做一个集合视图,根据月数自动更新(1个月-1行/ 5个月-5行)。
当我在条目中输入值,并按下Calculate
按钮时,什么也没有发生。也没有错误。
集合视图代码(XAML)
<CollectionView ItemsSource="{Binding MonthlyIncomeCalculus}">
<CollectionView.ItemTemplate>
<DataTemplate>
<Grid Padding="10">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Label Grid.Column="1"
Text="Month"
FontAttributes="Bold" />
<Label Grid.Column="2"
Text="Income with Rate"
FontAttributes="Bold" />
<Label Grid.Row="1"
Text="{Binding Month}"
FontAttributes="Bold" />
<Label Grid.Column="2"
Text="{Binding Income}"
FontAttributes="Bold" />
</Grid>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
可观察集合+“计算器”(C#)
public partial class HomePage
{
public HomePage()
{
InitializeComponent();
_ = new Entry { Keyboard = Keyboard.Numeric };
}
public class MonthlyIncome
{
public int Month { get; set; }
public double Income { get; set; }
}
public ObservableCollection<MonthlyIncome> MonthlyIncomeCalculus { get; set; } = new();
private void Calculate(object sender, EventArgs e)
{
BindingContext = MonthlyIncomeCalculus;
MonthlyIncomeCalculus.Clear();
MonthlyIncome monthi;
int Months = int.Parse(MonthInput.Text);
int Income = int.Parse(IncomeInput.Text);
for (int i = 0; i < Months; i++)
{
monthi = new MonthlyIncome();
monthi.Month = i + 1;
monthi.Income = (Income * Months) * 1.10;
MonthlyIncomeCalculus.Add(monthi);
}
}
}
1条答案
按热度按时间hs1ihplo1#
当你这样做
它告诉它在您的BindingContext中查找具有该名称的属性
但是由于该属性是您的BindingContext,因此您需要使用整个BindingContext作为ItemsSource
或者,也可以这样设置BindingContext