You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C# WPF类间变量传递问题:我的代码为何返回空值?

解决WPF中MainWindow向自定义控件传递变量后值为空的问题

问题场景

我需要将MainWindow类中的FirstName和LastName变量传递给自定义WPF控件Informations的同名变量,但运行后Informations里的这两个值始终为空,控件显示内容也为空。相关代码如下:

MainWindow类代码

using System.Windows;

namespace Test
{
    public partial class MainWindow : Window
    {
        #region Set Variables
        public string FirstName = "John";
        public string LastName = "Smith";
        #endregion

        public MainWindow()
        {
            InitializeComponent();
            PassVariable();
        }
        private void PassVariable()
        {
            Informations informations = new Informations();
            informations.FirstName = FirstName;
            informations.LastName = LastName;

            WrapPanel.Children.Add(informations); // 添加控件到WrapPanel,但显示为空
        }
    }
}

Informations类代码

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

namespace Test
{
    class Informations : Label
    {
        #region Set Variables
        public string FirstName { get { return FirstName; } set { FirstName = value; } }
        public string LastName { get { return LastName; } set { LastName = value; } }
        #endregion

        public Informations()
        {
            Foreground = Brushes.White;
            FontSize = 14;
            Content = FirstName + "\n" + LastName;
        }
    }
}

错误原因分析

  1. 属性递归调用,无实际存储字段:Informations类中的FirstName和LastName属性,getter直接返回自身、setter直接给自身赋值,属于无限递归调用,根本没有实际的私有字段存储值,导致赋值操作完全无效。
  2. Content初始化时机错误:Informations的构造函数在创建实例时就设置了Content,但此时FirstName和LastName还未被赋值(MainWindow中是先创建实例,再给属性赋值),后续赋值属性也不会自动更新Content。

修复方案

方案一:修复属性逻辑,添加值更新时的Content同步

修改Informations类,添加私有字段存储值,并在属性setter中同步更新Content:

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

namespace Test
{
    class Informations : Label
    {
        // 私有字段存储实际值
        private string _firstName;
        private string _lastName;

        public string FirstName 
        { 
            get { return _firstName; } 
            set 
            { 
                _firstName = value;
                // 属性值变化时更新控件内容
                UpdateDisplayContent();
            } 
        }
        public string LastName 
        { 
            get { return _lastName; } 
            set 
            { 
                _lastName = value;
                UpdateDisplayContent();
            } 
        }

        public Informations()
        {
            Foreground = Brushes.White;
            FontSize = 14;
            // 初始化时更新一次内容
            UpdateDisplayContent();
        }

        // 统一更新Content的方法,避免重复代码
        private void UpdateDisplayContent()
        {
            Content = $"{FirstName}\n{LastName}";
        }
    }
}

此方案适合后续需要动态修改FirstName或LastName并同步更新控件显示的场景。

方案二:通过构造函数直接传递参数

给Informations添加带参数的构造函数,创建实例时直接传入值,构造函数中设置Content:

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

namespace Test
{
    class Informations : Label
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }

        // 带参数的构造函数
        public Informations(string firstName, string lastName)
        {
            Foreground = Brushes.White;
            FontSize = 14;
            FirstName = firstName;
            LastName = lastName;
            Content = $"{FirstName}\n{LastName}";
        }
    }
}

同时修改MainWindow中的PassVariable方法:

private void PassVariable()
{
    Informations informations = new Informations(FirstName, LastName);
    WrapPanel.Children.Add(informations);
}

此方案更简洁,适合创建控件时就确定FirstName和LastName值的场景。

内容的提问来源于stack exchange,提问作者FrenchMan68

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.21 23:36:16