当我在 WPF C# 中按下按钮时自动更新 label.content
本文关键字:更新 content label 按钮 WPF | 更新日期: 2023-09-27 18:37:20
我创建了一个应用程序,其中有标签。当我单击按钮时,我需要更新标签内容。
XAML
<Label x:Name="lblTaxExcise" Content="Excise" HorizontalAlignment="Left" Margin="10,0,0,112" Grid.Row="2" Width="47" Height="29" VerticalAlignment="Bottom"/>
<Label x:Name="lblTaxEdu" Content="Edu. Cess" HorizontalAlignment="Left" Margin="10,0,0,78" Grid.Row="2" Width="68" Height="29" VerticalAlignment="Bottom"/>
<Label x:Name="lblTaxVat" Content="VAT" HorizontalAlignment="Left" Margin="10,0,0,44" Grid.Row="2" Width="35" Height="29" VerticalAlignment="Bottom"/>
C#
SqlCeCommand com1 = new SqlCeCommand("SELECT ExciseTax, EduCessTax, VatTax FROM Tax_Master", con);
SqlCeDataReader dr1 = com1.ExecuteReader();
while (dr1.Read())
{
Excise = Convert.ToInt32(dr1[0]);
EduCess = Convert.ToInt32(dr1[1]);
Vat = Convert.ToInt32(dr1[2]);
}
lblTaxExcise.Content = "Excise @ " + Excise;
lblTaxEdu.Content = "Edu. Cess @ " + EduCess;
lblTaxVat.Content = "VAT @ " + Vat;
部分值是从数据库中获取的。
只需删除标签的宽度即可。它应该工作
<Label x:Name="lblTaxExcise" Content="Excise" HorizontalAlignment="Left" Margin="10,0,0,112" Grid.Row="2" Height="29" VerticalAlignment="Bottom"/>
<Label x:Name="lblTaxEdu" Content="Edu. Cess" HorizontalAlignment="Left" Margin="10,0,0,78" Grid.Row="2" Height="29" VerticalAlignment="Bottom"/>
<Label x:Name="lblTaxVat" Content="VAT" HorizontalAlignment="Left" Margin="10,0,0,44" Grid.Row="2" Height="29" VerticalAlignment="Bottom"/>
将标签宽度属性更改为自动。