C# WPF之控件(Button、Textbox、Checkbox、ComboBox)与布局(Grid、StackPanel)的基本使用

.xaml 的定义如下:

<Window x:Class="WPF_APP.TheBasics"
        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:WPF_APP"
        mc:Ignorable="d"
        Loaded="Window_Loaded"
        Title="TheBasics" Height="750" Width="400">
    <Grid>
        <Border Padding="10">
            <StackPanel>
                <!--Buttons-->
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="*"/>
                    </Grid.ColumnDefinitions>
                    <Button x:Name="ApplyButton" Click="ApplyButton_Click" Margin ="0,0,10,0" Grid.Column="0" Content="Apply"></Button>
                    <Button x:Name="ResetButton" Click="ResetButton_Click" Grid.Column="1" Content="Reset"></Button>
                    <Button x:Name="RefreshButton" Margin ="10 0 0 0" Grid.Column="2" Content="Refresh"></Button>
                </Grid>
                <TextBlock Text="pulse Properties" FontWeight="Bold" Margin="0 10"/>
                <!--Desctrption-->
                <TextBlock Text="Desctrption"/>
                <TextBox x:Name="DescriptionText" Padding="2"/>
                <!--Status and Revise-->
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="2*"/>
                        <ColumnDefinition Width="*"/>
                    </Grid.ColumnDefinitions>
                    <!--status-->
                    <StackPanel Grid.Column="0" Margin="0 0 10 0">
                        <TextBlock Text="Status"/>
                        <TextBox IsReadOnly="True" Background="#eee" Padding="2"/>
                    </StackPanel>
                    <StackPanel Grid.Column="1">
                        <TextBlock Text="Revision"/>
                        <TextBox IsReadOnly="True" Background="#eee" Padding="2"/>
                    </StackPanel>
                </Grid>
                <!--Part Number-->
                <TextBlock Text="Part Number"/>
                <TextBox IsReadOnly="True" Background="#eee" Padding="2"/>
                <!--Raw Material-->
                <TextBlock Text="Raw Material" FontWeight="Bold" Margin="0 10"/>
                <!--Material-->
                <TextBlock Text="Material"/>
                <ComboBox Padding="2"/>
                <!--Manufacturing Info-->
                <TextBlock Text="Manufacturing Info" FontWeight="Bold" Margin="0 10"/>
                <!--Work Centres-->
                <TextBlock Text="Work Centres"/>
                <!--CheckBoxs-->
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="*"/>
                    </Grid.ColumnDefinitions>
                    <!--column1-->
                    <StackPanel Grid.Column="0" Margin="0 0 10 0">
                        <CheckBox Checked="Checkbox_checked" x:Name="WeldCheckBox" Content="Weld"/>
                        <CheckBox Checked="Checkbox_checked" x:Name="AssemblyCheckBox" Content="Assembly"/>
                        <CheckBox Checked="Checkbox_checked" x:Name="PlasmaCheckBox" Content="Plasma"/>
                        <CheckBox Checked="Checkbox_checked" x:Name="LaserCheckBox" Content="Laser"/>
                        <CheckBox Checked="Checkbox_checked" x:Name="PurchaseCheckBox" Content="Purchase"/>
                    </StackPanel>
                    <!--column2-->
                    <StackPanel Grid.Column="1">
                        <CheckBox Checked="Checkbox_checked" x:Name="LatheCheckBox" Content="Lathe"/>
                        <CheckBox Checked="Checkbox_checked" x:Name="DrillCheckBox" Content="Drill"/>
                        <CheckBox Checked="Checkbox_checked" x:Name="FoldCheckBox" Content="Fold"/>
                        <CheckBox Checked="Checkbox_checked" x:Name="RollCheckBox" Content="Roll"/>
                        <CheckBox Checked="Checkbox_checked" x:Name="SawCheckBox" Content="Saw"/>
                    </StackPanel>
                </Grid>
                <!--Length-->
                <TextBlock Text="Length"/>
                <TextBox x:Name="LengthText" Padding="2"/>
                <!--Mass-->
                <TextBlock Text="Mass"/>
                <TextBox x:Name="MessText" IsReadOnly="True" Background="#eee" Padding="2"/>
                <!--Finish-->
                <TextBlock Text="Finish"/>
                <ComboBox x:Name="FinishDropdown" SelectionChanged="FinishDropdown_SelectionChanged" SelectedIndex="0 " Padding="2">
                    <ComboBoxItem>Painted</ComboBoxItem>
                    <ComboBoxItem>Not Painted</ComboBoxItem>
                </ComboBox>
                <!--Purchase Info-->
                <TextBlock Text="Purchase Infomation"/>
                <ComboBox SelectedIndex="0 " Padding="2">
                    <ComboBoxItem>Rubber</ComboBoxItem>
                    <ComboBoxItem>Not Rubber</ComboBoxItem>
                </ComboBox>
                <!--Supplier Name-->
                <TextBlock Text="Supplier Name"/>
                <TextBox x:Name="SupplierNameText" TextChanged="SupplierNameText_TextChanged" Padding="2"/>
                <!--Supplier Code-->
                <TextBlock Text="Supplier Code"/>
                <TextBox Padding="2"/>
                <TextBlock Text="Additional Info" FontWeight="Bold" Margin="0 10"/>
                <!--Note-->
                <TextBlock Text="Note"/>
                <TextBox x:Name="NoteText" Padding="2"/>

            </StackPanel>
        </Border>
    </Grid>
</Window>

.cs 的定义如下:

using System.Windows;
using System.Windows.Controls;

namespace WPF_APP
{
    /// <summary>
    /// TheBasics.xaml 的交互逻辑
    /// </summary>
    public partial class TheBasics : Window
    {
        public TheBasics()
        {
            InitializeComponent();
        }

        private void ApplyButton_Click(object sender, RoutedEventArgs e)
        {
            //输出填写的内容
            MessageBox.Show($"the description is:{this.DescriptionText.Text}");
        }

        private void ResetButton_Click(object sender, RoutedEventArgs e)
        {
            //取消勾选
            this.WeldCheckBox.IsChecked = this.AssemblyCheckBox.IsChecked = this.PlasmaCheckBox.IsChecked = this.LaserCheckBox.IsChecked =
                this.PurchaseCheckBox.IsChecked = this.LatheCheckBox.IsChecked = this.DrillCheckBox.IsChecked = this.FoldCheckBox.IsChecked =
                this.RollCheckBox.IsChecked = this.SawCheckBox.IsChecked = false;
        }

        private void Checkbox_checked(object sender, RoutedEventArgs e)
        {
            //获取对象的内容
            this.LengthText.Text += ((CheckBox)sender).Content;
        }

        private void FinishDropdown_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (this.NoteText == null)
                return;
            var combobox = (ComboBox)sender;
            var value = (ComboBoxItem)combobox.SelectedValue;
            this.NoteText.Text = (string)value.Content;
        }

        //窗口加载就执行事件
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            FinishDropdown_SelectionChanged(this.FinishDropdown, null);
        }

        private void SupplierNameText_TextChanged(object sender, TextChangedEventArgs e)
        {
            this.MessText.Text = this.SupplierNameText.Text;
        }
    }
}

最后,显示效果:

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值