杨条儿
2021-07-16 16:46
采纳率: 66.7%
浏览 28

wpf多界面传输数据

以wpf为前台 c#为后台
我在界面一的后台计算出数据aa,bb
希望在界面二展示
求运用c#中的类来解决

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • 砻谷踏碓 2021-07-16 16:56
    已采纳

    看你问了几次了

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.ComponentModel;
    
    namespace WpfApplication1
    {
        class Dataabb:INotifyPropertyChanged
        {
            protected void ech<T>(ref T propValue, T newValue, string propName = "");
    
            int m_aa;
            public int aa
            {
                get { return m_aa; }
                set
                {
                    ech(ref m_aa, value);
                }
            }
    
            int m_bb;
            public int bb
            {
                get { return m_bb; }
                set
                {
                    ech(ref m_bb, value);
                }
            }
        }
    }
    
    
    
    已采纳该答案
    1 打赏 评论
  • 自在猫先生 2021-07-16 16:59

    界面2定义一个公共变量,打开界面2的时候把界面1的值赋值给这个变量,就获取到界面1的值了。

    1 打赏 评论
  • 砻谷踏碓 2021-07-16 20:11

    更详细供参考

    Dataabb数据模型

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.ComponentModel;
    
    using System.Runtime.CompilerServices;
    
    
    namespace WpfApplication1
    {
        public class Dataabb:INotifyPropertyChanged
        {
            public event PropertyChangedEventHandler PropertyChanged;
    
            protected void ech<T>(ref T propValue, T newValue, [CallerMemberName] string propName = "")
            {
                if (object.Equals(propValue, newValue)) return;
    
                propValue = newValue;
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(propName));
                }
            }
    
            int m_aa;
            public int aa
            {
                get { return m_aa; }
                set
                {
                    ech(ref m_aa, value);
                }
            }
    
            int m_bb;
            public int bb
            {
                get { return m_bb; }
                set
                {
                    ech(ref m_bb, value);
                }
            }
        }
    }
    
    

    Window1代码

    
        public partial class Window1 : Window
        {
            public Dataabb dataabb = new Dataabb();
            public Window1()
            {
                InitializeComponent();
                dataabb.aa = 34;
                dataabb.bb = 55;
                Binding bdaa = new Binding() { Path = new PropertyPath("aa"), Source = dataabb };
                textBox1.SetBinding(TextBox.TextProperty, bdaa);
                textBox3.SetBinding(TextBox.TextProperty, bdaa);
    
                Window2 win2 = new Window2();
                win2.textBox1.SetBinding(TextBox.TextProperty, bdaa);
    
                Binding bdbb = new Binding() { Path = new PropertyPath("bb"), Source = dataabb };
                textBox2.SetBinding(TextBox.TextProperty, bdbb);
                win2.textBox2.SetBinding(TextBox.TextProperty, bdbb);
    
                win2.Show();
            }
        }
    

    运行效果

    img

    1 打赏 评论

相关推荐 更多相似问题