【发布时间】:2016-11-22 19:50:53
【问题描述】:
我有以下 xaml:
<GroupBox x:Name="GroupBoxInworp" IsEnabled="True" Header="Inworp" HorizontalAlignment="Stretch" Margin="10,120,10,0" VerticalAlignment="Top" VerticalContentAlignment="Stretch">
<StackPanel x:Name="StackPanelInworp" Button.Click="button_Click">
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Top" Margin="0">
<Button x:Name="btnTweeEuro" Margin="10" MinWidth="220" Content="2" Click="btnTweeEuro_Click"></Button>
<Button x:Name="btnEenEuro" Margin="10" MinWidth="220" Content="1" Click="btnEenEuro_Click"></Button>
</StackPanel>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Top" Margin="0">
<Button x:Name="btnVijftigEurocent" Margin="10" MinWidth="100" Content="50" Click="btnVijftigEurocent_Click"></Button>
<Button x:Name="btnTwintigEurocent" Margin="10" MinWidth="100" Content="20" Click="btnTwintigEurocent_Click"></Button>
<Button x:Name="btnTienEurocent" Margin="10" MinWidth="100" Content="10" Click="btnTienEurocent_Click"></Button>
<Button x:Name="btnVijfEurocent" Margin="10" MinWidth="100" Content="5" Click="btnVijfEurocent_Click"></Button>
</StackPanel>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Top" Margin="5">
<Label Content="Huidige inworp:" MinWidth="160"></Label>
<Label x:Name="lblHuidigeInworp" Content="" MinWidth="300" HorizontalContentAlignment="Right"></Label>
</StackPanel>
</StackPanel>
</GroupBox>
我有以下代码:
private void GeldClick(decimal Inworp)
{
TotaalInworp += Inworp;
lblHuidigeInworp.Content = String.Format("€ {0}", TotaalInworp);
}
private void btnTweeEuro_Click(object sender, RoutedEventArgs e)
{
GeldClick(2.00M);
}
private void btnEenEuro_Click(object sender, RoutedEventArgs e)
{
GeldClick(1.00M);
}
private void btnVijftigEurocent_Click(object sender, RoutedEventArgs e)
{
GeldClick(0.50M);
}
private void btnTwintigEurocent_Click(object sender, RoutedEventArgs e)
{
GeldClick(0.20M);
}
private void btnTienEurocent_Click(object sender, RoutedEventArgs e)
{
GeldClick(0.10M);
}
private void btnVijfEurocent_Click(object sender, RoutedEventArgs e)
{
GeldClick(0.05M);
}
我想为所有带有 for 或 switch 的点击事件创建一个处理程序。
处理程序就像
StackPanelInworp.AddHandler(Button.ClickEvent, new RoutedEventHandler(button_Click));
我还想创建一个类,我可以在其中放置带有 for 或 switch 的事件处理程序。有 6 个按钮。
如何做到这一点?另外,它涉及小数,所以我认为切换是不可能的?
【问题讨论】:
标签: class for-loop button click switch-statement