stackpanel中多个按钮的单击处理程序

时间:2016-11-22 19:50:54

标签: class for-loop button click switch-statement

我关注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个按钮。

如何实现这一目标?此外,它涉及小数,所以我认为切换是不可能的吗?

1 个答案:

答案 0 :(得分:0)

我想出了这个: 的的Xaml

<StackPanel x:Name="StackPanelInworp">
            <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Top" Margin="0">
                <Button x:Name="btnTweeEuro" Margin="10" MinWidth="220" Content="2" Click="btnGeld_Click"></Button>
                <Button x:Name="btnEenEuro" Margin="10" MinWidth="220" Content="1" Click="btnGeld_Click"></Button>
            </StackPanel>
            <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Top" Margin="0">
                <Button x:Name="btnVijftigEurocent" Margin="10" MinWidth="100" Content="50" Click="btnGeld_Click"></Button>
                <Button x:Name="btnTwintigEurocent" Margin="10" MinWidth="100" Content="20" Click="btnGeld_Click"></Button>
                <Button x:Name="btnTienEurocent" Margin="10" MinWidth="100" Content="10" Click="btnGeld_Click"></Button>
                <Button x:Name="btnVijfEurocent" Margin="10" MinWidth="100" Content="5" Click="btnGeld_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>

代码:

//Methode voor uitvoeren van alle click events mbt Geld
    private void GeldClick(decimal Inworp)
    {
        TotaalInworp += Inworp;
        lblHuidigeInworp.Content = String.Format("€ {0}", TotaalInworp); 
    }

    //Enkel click event voor alle knoppen mbt Drank
    private void btnGeld_Click(object sender, EventArgs e)
    {
        Button btn = sender as Button;
        switch (btn.Content.ToString())
        {
            case "2":
                GeldClick(2.00M);
                break;
            case "1":
                GeldClick(1.00M);
                break;
            case "50":
                GeldClick(0.50M);
                break;
            case "20":
                GeldClick(0.20M);
                break;
            case "10":
                GeldClick(0.10M);
                break;
            case "5":
                GeldClick(0.05M);
                break;
        }
    }

有谁知道改进?想要使用stackpanelname来获取按钮,但不知道如何...