首页 > 解决方案 > 根据组合框的值计算文本框的值?(wpf)

问题描述

我有一个小程序,因为我是新手,我的程序是通过组合框选项将数据从 sql 服务器带到文本框,并使用该文本框中显示的值来计算 + 我已经完成了放置数据的步骤,现在感谢你帮我计算文本框中的值,谢谢你的帮助。xml代码:

<Window x:Class="comboboxapp1.MainWindow"
        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:comboboxapp1"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">

    <Window.Resources>
        <local:SimpleMath x:Key="MyFriends"/>
    </Window.Resources>

    <Grid>
        <Label Content="code" HorizontalAlignment="Left" Margin="38,52,0,0" 
               VerticalAlignment="Top" Width="46" Height="23"/>
        <Label Content="pieces" HorizontalAlignment="Left" Margin="38,126,0,0" 
               VerticalAlignment="Top" Width="46" Height="23"/>
        <Label Content="layers" HorizontalAlignment="Left" Margin="38,196,0,0" 
               VerticalAlignment="Top" Width="46" Height="30"/>
        <Label Content="production pieces" HorizontalAlignment="Left" 
               Margin="0,278,0,0" VerticalAlignment="Top" Width="108" Height="25"/>
        <TextBox x:Name="txtcode" 
                 Text="{Binding Txtcode, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
                 HorizontalAlignment="Left" Height="23" Margin="124,52,0,0" 
                 TextWrapping="Wrap" VerticalAlignment="Top" Width="141"/>
        <TextBox x:Name="txtpieces" 
                 Text="{Binding Txtpieces, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" 
                 HorizontalAlignment="Left" Height="23" 
                 Margin="124,133,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="141"/>
        <TextBox x:Name="txtlayers" 
                 Text="{Binding Txtlayers,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" 
                 HorizontalAlignment="Left" Height="23" 
                 Margin="124,203,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="141"/>
        <TextBox x:Name="txtproductionpieces" 
                 Text="{Binding Txtproductionpieces,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" 
                 HorizontalAlignment="Left" Height="23" Margin="124,280,0,0" 
                 TextWrapping="Wrap" VerticalAlignment="Top" Width="141"/>
        <ComboBox x:Name="comboBox1" ItemsSource="{Binding Source={StaticResource MyFriends}}" 
                  HorizontalAlignment="Left" Margin="418,52,0,0" VerticalAlignment="Top" 
                  Width="319" Height="36" SelectionChanged="ComboBox1_SelectionChanged"/>
        <TextBox x:Name="txtseccond" 
                 Text="{Binding Txtseccond,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" 
                 HorizontalAlignment="Left" Height="23" 
                 Margin="124,345,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="141"/>
        <Label Content="seccond" HorizontalAlignment="Left" Margin="38,345,0,0" 
               VerticalAlignment="Top" Width="46" Height="23"/>
        <TextBlock Text="{Binding A, Mode=OneWay,UpdateSourceTrigger=PropertyChanged}" 
                   HorizontalAlignment="Left" Margin="418,133,0,0" TextWrapping="Wrap"  
                   VerticalAlignment="Top" Height="23" Width="248"/>
        <TextBox Text="{Binding No1,Mode=OneWay,UpdateSourceTrigger=PropertyChanged}" 
                 HorizontalAlignment="Left" Height="21" Margin="426,210,0,0" 
                 TextWrapping="Wrap" VerticalAlignment="Top" Width="303"/>
    </Grid>
</Window>

C#代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data.SqlClient;
using System.ComponentModel;
using System.Runtime.CompilerServices;

namespace comboboxapp1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public SimpleMath Formular { get; set; }
        public object SelectedValue { get; private set; }

        public MainWindow()
        {
            Formular = new SimpleMath()
            {
                Txtcode = 0,
                Txtpieces = 0,
                Txtlayers = 0,
                Txtproductionpieces = 0,
                Txtseccond = 0,

            };
            InitializeComponent();
            DataContext = Formular;
            Fillcombobox();
        }
        private void MainWindow_Load(object sender, EventArgs e) 
        {

        }
        public void Fillcombobox()
        {
            SqlConnection con = new SqlConnection("Data Source=LEAN-22\\SQLEXPRESS;Initial 
Catalog=LUAT;Integrated Security=True");

            string sql = " select * from comboboxnew ";
            SqlCommand cmd = new SqlCommand(sql, con);
            SqlDataReader myreader;
            try
            {
                con.Open();
                myreader = cmd.ExecuteReader();
                while (myreader.Read())
                {
                    string sname = myreader.GetInt32(0).ToString();
                    comboBox1.Items.Add(sname);

                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);

            }
        }
        public class SimpleMath : INotifyPropertyChanged
        {
            private int no1;

            public int No1
            {
                get { return no1; }
                set
                {
                    no1 = value;
                    OnPropertyChanged("No1");
                    OnPropertyChanged("A");
                }
            }
            private int txtcode;

            public int Txtcode
            {
                get { return txtcode; }
                set
                {
                    txtcode = value;
                    OnPropertyChanged("Txtcode");
                    OnPropertyChanged("A");
                }
            }
            private int txtpieces;

            public int Txtpieces
            {
                get { return txtpieces; }
                set
                {
                    txtpieces = value;
                    OnPropertyChanged("Txtcode");
                    OnPropertyChanged("A");
                }
            }
            private int txtlayers;

            public int Txtlayers
            {
                get { return txtlayers; }
                set
                {
                    txtlayers = value;
                    OnPropertyChanged("Txtlayers");
                    OnPropertyChanged("A");
                }
            }
            private int txtproductionpieces;

            public int Txtproductionpieces
            {
                get { return txtproductionpieces; }
                set
                {
                    txtproductionpieces = value;
                    OnPropertyChanged("Txtproductionpieces");
                    OnPropertyChanged("A");
                }
            }
            private int txtseccond;

            public int Txtseccond
            {
                get { return txtseccond; }
                set
                {
                    txtseccond = value;
                    OnPropertyChanged("Txtseccond");
                    OnPropertyChanged("A");
                }
            }
            public double A => No1;

            public event PropertyChangedEventHandler PropertyChanged;
            public void OnPropertyChanged([CallerMemberName()] string propertyName = null)
            {
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        private void ComboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

            SqlConnection con = new SqlConnection("Data Source=LEAN-22\\SQLEXPRESS;Initial 
Catalog=LUAT;Integrated Security=True");
            //  string sql = " select * from comboboxnew where code = '" + comboBox1.Text+ "';";
            string sql = " select * from comboboxnew where code = '" + comboBox1.SelectedItem + 
"';";
            //Console.WriteLine(comboBox1.Text);
            //MessageBox.Show(comboBox1.Text);
            SqlCommand cmd = new SqlCommand(sql, con); 
            SqlDataReader myreader; 
            try
            {
                con.Open();
                myreader = cmd.ExecuteReader();
                while (myreader.Read())
                {
                    string code = myreader.GetInt32(0).ToString();
                    string pieces = myreader.GetInt32(1).ToString();
                    string layers = myreader.GetInt32(2).ToString();
                    string productionpieces = myreader.GetInt32(3).ToString();
                    string seccond = myreader.GetInt32(4).ToString();
                     txtcode.Text = code;
                    //txtcode.Text =SelectedValue;
                    txtpieces.Text = pieces;
                      //txtpieces.Text = "New value";
                    txtlayers.Text = layers;
                       //txtlayers.Text = "New value";
                    txtproductionpieces.Text = productionpieces;
                     //txtproductionpieces.Text = "New value";
                    txtseccond.Text = seccond;
                    //txtseccond.Text = "New value";

                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}  

在此处输入图像描述

标签: c#wpfcomboboxtextbox

解决方案


它不能 caculator 显示文本框总和 c# 代码:

using System;

using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data.SqlClient;
using System.ComponentModel;
using System.Runtime.CompilerServices;

namespace comboboxapp1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
           public MainWindow()
        { 
            InitializeComponent();
            this.DataContext = new SimpleMath();
             Fillcombobox();
        }
            private void MainWindow_Load(object sender, EventArgs e)
            {

            }
            public void Fillcombobox()
            {
            SqlConnection con = new SqlConnection("Data Source=LEAN-22\\SQLEXPRESS;Initial Catalog=LUAT;Integrated Security=True") ;

                string sql = " select * from comboboxnew ";
                SqlCommand cmd = new SqlCommand(sql, con);
                SqlDataReader myreader;
                try
                {
                    con.Open();
                    myreader = cmd.ExecuteReader();
                    while (myreader.Read())
                    {
                        string sname = myreader.GetInt32(0).ToString();
                        comboBox1.Items.Add(sname);

                    }

                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);

                }
            }
        public class SimpleMath : INotifyPropertyChanged
        {
            private int _no1;

            public int No1
            {
                get => _no1;
                set
                {
                    _no1 = value;
                    OnPropertyChanged();
                    CalculateSum();
                    OnPropertyChanged("A");
                }
            }
            private int _txtcode;

            public int Txtcode
            {
                get => _txtcode;
                set
                {
                    _txtcode = value;
                    OnPropertyChanged();
                    CalculateSum();
                    OnPropertyChanged("A");
                }
            }
            private int _txtpieces;

            public int Txtpieces
            {
                get => _txtpieces;
                set
                {
                    _txtpieces = value;
                    OnPropertyChanged();
                    CalculateSum();
                    OnPropertyChanged("A");

                }
            }
            private int _txtlayers;

            public int Txtlayers
            {
                get => _txtlayers;
                set
                {
                    _txtlayers = value;
                    OnPropertyChanged();
                    CalculateSum();
                    OnPropertyChanged("A");

                }
            }
            private int _txtproductionpieces;

            public int Txtproductionpieces
            {
                get => _txtproductionpieces;
                set
                {
                    _txtproductionpieces = value;
                    OnPropertyChanged();
                    CalculateSum();
                    OnPropertyChanged("A");

                }
            }
            private int _txtseccond;

            public int Txtseccond
            {
                get => _txtseccond;
                set
                {
                    _txtseccond = value;
                    OnPropertyChanged();
                    CalculateSum();
                    OnPropertyChanged("A");

                }
            }
            private int a;
            public int A
            {
                get => a;
                set
                {
                    a = value;
                    OnPropertyChanged();
                }
            }
            private void CalculateSum()
            {
                A = Txtcode + Txtlayers + Txtpieces;
            }
            public event PropertyChangedEventHandler PropertyChanged;
            public void OnPropertyChanged([CallerMemberName()] string propertyName = null)
            {
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        private void ComboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {


            SqlConnection con = new SqlConnection("Data Source=LEAN-22\\SQLEXPRESS;Initial Catalog=LUAT;Integrated Security=True");
            //  string sql = " select * from comboboxnew where code = '" + comboBox1.Text+ "';";
            string sql = " select * from comboboxnew where code = '" + comboBox1.SelectedItem + "';";

            //Console.WriteLine(comboBox1.Text);
            //MessageBox.Show(comboBox1.Text);
            SqlCommand cmd = new SqlCommand(sql, con); 
            SqlDataReader myreader; 
            try
            {

                con.Open();
                myreader = cmd.ExecuteReader();
                while (myreader.Read())
                {

                    string code = myreader.GetInt32(0).ToString();
                    string pieces = myreader.GetInt32(1).ToString();
                    string layers = myreader.GetInt32(2).ToString();
                    string productionpieces = myreader.GetInt32(3).ToString();
                    string seccond = myreader.GetInt32(4).ToString();

                    txtcode.Text = code;

                    txtpieces.Text = pieces;

                    txtlayers.Text = layers;

                    txtproductionpieces.Text = productionpieces;

                    txtseccond.Text = seccond;




            }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

    }
}  

xml代码:

    <Window x:Class="comboboxapp1.MainWindow"
        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:comboboxapp1"
        xmlns:dxa="http://schemas.devexpress.com/winfx/2008/xaml/accordion" xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core" xmlns:dxc="http://schemas.devexpress.com/winfx/2008/xaml/charts" xmlns:dxlc="http://schemas.devexpress.com/winfx/2008/xaml/layoutcontrol" 
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
      <Grid>
        <Label Content="code" HorizontalAlignment="Left" Margin="38,52,0,0" VerticalAlignment="Top" Width="46" Height="23"/>
        <Label Content="pieces" HorizontalAlignment="Left" Margin="38,126,0,0" VerticalAlignment="Top" Width="46" Height="23"/>
        <Label Content="layers" HorizontalAlignment="Left" Margin="38,196,0,0" VerticalAlignment="Top" Width="46" Height="30"/>
        <Label Content="production pieces" HorizontalAlignment="Left" Margin="0,278,0,0" VerticalAlignment="Top" Width="108" Height="25"/>
        <TextBox x:Name="txtcode" Text="{Binding Txtcode, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Left" Height="23" Margin="124,52,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="141"/>
        <TextBox x:Name="txtpieces" Text="{Binding Txtpieces, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Left" Height="23" Margin="124,133,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="141"/>
        <TextBox x:Name="txtlayers" Text="{Binding Txtlayers,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Left" Height="23" Margin="124,203,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="141"/>
        <TextBox x:Name="txtproductionpieces" Text="{Binding Txtproductionpieces,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Left" Height="23" Margin="124,280,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="141"/>
        <ComboBox x:Name="comboBox1" Text="{Binding combobox1,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Left" Margin="418,52,0,0" VerticalAlignment="Top" Width="319" Height="36" SelectionChanged="ComboBox1_SelectionChanged"/>
        <TextBox x:Name="txtseccond" Text="{Binding Txtseccond,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"  HorizontalAlignment="Left" Height="23" Margin="124,345,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="141"/>
        <Label Content="seccond" HorizontalAlignment="Left" Margin="38,345,0,0" VerticalAlignment="Top" Width="46" Height="23"/>
        <TextBlock Text="{Binding A, Mode=OneWay,UpdateSourceTrigger=PropertyChanged}"  HorizontalAlignment="Left" Margin="418,133,0,0" TextWrapping="Wrap"  VerticalAlignment="Top" Height="23" Width="248"/>
        <TextBox Text="{Binding No1,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Left" Height="21" Margin="426,210,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="303"/>


    </Grid>
</Window>

我的数据 sql: 在此处输入图像描述


推荐阅读