如何在WPF中的组框内添加多个按钮?

时间:2018-12-01 20:29:56

标签: wpf button groupbox

我需要在wpf的组框内添加多个按钮,但我不能。我需要为自动取款机创建带有所有数字的键盘,但是如果我尝试创建2号按钮,则1号按钮会消失。 怎么了?

<Window x:Name="Win_Users" x:Class="ATM_Simulator.WindowUsers"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:ATM_Simulator"
    mc:Ignorable="d"
    Title="WindowUsers" Height="300" Width="500">
<Grid>
    <GroupBox x:Name="grbx_key_pad" Header="Key Pad" HorizontalAlignment="Left" Height="220" Margin="26,10,0,0" VerticalAlignment="Top" Width="189">
        <Button x:Name="btn_1" Content="1" HorizontalAlignment="Left" Margin="10,13,0,0" VerticalAlignment="Top" Width="29" Height="32"/>
    </GroupBox>
    <GroupBox x:Name="grbx_select_transaction" Header="Select Transaction :" HorizontalAlignment="Left" Height="88" Margin="291,23,0,0" VerticalAlignment="Top" Width="179"/>
    <GroupBox x:Name="grbx_select_account" Header="Select Account" HorizontalAlignment="Left" Height="92" Margin="291,138,0,0" VerticalAlignment="Top" Width="179"/>
    <Button x:Name="btn_submit" Content="Submit" HorizontalAlignment="Left" Margin="291,239,0,0" VerticalAlignment="Top" Width="75"/>
    <Button x:Name="btn_close" Content="Close" HorizontalAlignment="Left" Margin="393,239,0,0" VerticalAlignment="Top" Width="77"/>
</Grid>

1 个答案:

答案 0 :(得分:0)

您必须在GroupBox内放置一个容器,例如,网格是一个不错的选择:

<GroupBox x:Name="grbx_key_pad" Header="Key Pad" HorizontalAlignment="Left" Height="220" Margin="26,10,0,0" VerticalAlignment="Top" Width="189">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
            <ColumnDefinition/>
         </Grid.ColumnDefinitions> 
        <Button x:Name="btn_1" Grid.Column="0" Content="1" Width="29" Height="32"/>
        <Button x:Name="btn_2" Grid.Column="1" Content="2" Width="29" Height="32"/>
        <!-- more buttons -->
        <Button x:Name="btn_4" Grid.Row="1" Grid.Column="0" Content="4" Width="29" Height="32"/>
        <!-- yet more buttons -->
    </Grid>
</GroupBox>