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

XAML属性绑定C#后台变量失效,求正确实现方式

正确实现XAML与C#后台变量的x:Bind绑定

嘿,我帮你找出问题所在啦,这就给你纠正并说明正确的绑定方式:

你当前代码的几个核心问题

  • 局部变量无法被x:Bind访问:你在构造函数里定义的height_background等都是局部变量,x:Bind只能绑定到页面类的公共成员(字段或属性),根本找不到这些局部变量。
  • x:Bind语法错误:绑定必须用大括号包裹,比如你写的Text="x:bind text_textbox1"是错误的,正确写法是Text="{x:Bind text_textbox1}",其他Width/Height的绑定也犯了同样的错。
  • 语法小问题:构造函数里的string text_textbox1 = "string test"末尾少了分号,这会直接导致编译失败。

修正后的完整代码

后台C#代码

把变量改成页面类的公共成员(推荐用属性,字段也可以但属性更符合WPF/UWP的绑定规范):

namespace Test
{
    public sealed partial class MainPage : Page
    {
        // 定义为公共属性(或公共字段),让x:Bind可以访问
        public int height_background { get; set; }
        public int width_background { get; set; }
        public int height_border { get; set; }
        public int width_border { get; set; }
        public string text_textbox1 { get; set; }

        public MainPage()
        {
            this.InitializeComponent();
            // 在构造函数中给这些成员赋值
            height_background = 500;
            width_background = 600;
            height_border = 500;
            width_border = 600;
            text_textbox1 = "string test";
        }
    }
}

XAML代码

修正绑定的语法,用大括号包裹x:Bind表达式:

<Page Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" 
      Width="{x:Bind width_background}" 
      Height="{x:Bind height_background}">
    <Grid>
        <TextBox x:Name="Textbox1" Text="{x:Bind text_textbox1}" />
        <Border BorderThickness="1" 
                Height="{x:Bind height_border}" 
                Width="{x:Bind width_border}"/>
    </Grid>
</Page>

进阶:实现动态更新(值变化时界面自动刷新)

如果之后你需要在代码中修改这些值,并且希望界面自动同步更新,那页面类需要实现INotifyPropertyChanged接口,示例如下:

using System.ComponentModel;
using System.Runtime.CompilerServices;

namespace Test
{
    public sealed partial class MainPage : Page, INotifyPropertyChanged
    {
        // 用私有字段+公共属性的方式,配合通知事件
        private int _height_background;
        public int height_background
        {
            get => _height_background;
            set
            {
                _height_background = value;
                OnPropertyChanged(); // 通知界面属性已更新
            }
        }

        private string _text_textbox1;
        public string text_textbox1
        {
            get => _text_textbox1;
            set
            {
                _text_textbox1 = value;
                OnPropertyChanged();
            }
        }

        // 其他属性(width_background、height_border等)同理,按照上面的写法修改

        // 实现INotifyPropertyChanged接口的事件和方法
        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        public MainPage()
        {
            this.InitializeComponent();
            height_background = 500;
            text_textbox1 = "string test";
            // 其他属性赋值...
        }
    }
}

这样之后,只要你在代码中修改height_background等属性的值,界面就会自动刷新显示新的内容啦。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.07 00:22:38