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

如何从其他页面绑定TextBlock的Text属性

跨页面绑定TextBlock内容的实现方案

方法1:共享ObservableViewModel(推荐)

这是MVVM架构下的标准实现,能自动通知UI数据变更。

  1. 创建实现INotifyPropertyChanged的共享ViewModel类:
using System.ComponentModel;
using System.Runtime.CompilerServices;

public class SharedViewModel : INotifyPropertyChanged
{
    private string _someValue;
    public string SomeValue
    {
        get => _someValue;
        set
        {
            _someValue = value;
            OnPropertyChanged();
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}
  1. 在App.xaml.cs中初始化全局实例:
public static SharedViewModel SharedVM { get; } = new SharedViewModel();
  1. 在ShellPage.xaml.cs中设置值:
App.SharedVM.SomeValue = "Some text...";
  1. 在Page2.xaml中绑定全局ViewModel(需添加项目命名空间引用,比如xmlns:local="clr-namespace:你的项目命名空间"):
<TextBlock Text="{Binding Source={x:Static local:App.SharedVM}, Path=SomeValue}" 
           Style="{ThemeResource SubtitleTextBlockStyle}" />

方法2:通过页面导航传递DataContext

如果Page2是ShellPage导航打开的子页面,可直接传递ShellPage的上下文。

  1. 在ShellPage.xaml.cs中实现属性通知:
public sealed partial class ShellPage : Page, INotifyPropertyChanged
{
    private string _someValue;
    public string SomeValue
    {
        get => _someValue;
        set
        {
            _someValue = value;
            OnPropertyChanged();
        }
    }

    public ShellPage()
    {
        this.InitializeComponent();
        this.DataContext = this;
    }

    // 示例赋值方法
    private void UpdateText()
    {
        SomeValue = "Some text...";
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}
  1. ShellPage中导航到Page2时传递上下文:
// 假设ShellPage里的导航Frame名为MainFrame
MainFrame.Navigate(typeof(Page2), this.DataContext);
  1. 在Page2.xaml.cs中接收并设置DataContext:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
    base.OnNavigatedTo(e);
    if (e.Parameter is ShellPage pageContext)
    {
        this.DataContext = pageContext;
    }
}
  1. Page2.xaml的绑定保持你原本的写法即可:
<TextBlock Text="{Binding SomeValue}" Style="{ThemeResource SubtitleTextBlockStyle}" />

方法3:静态类快速实现(适合简单场景)

如果不需要自动更新UI,可直接用静态字段:

  1. 创建静态存储类:
public static class AppStaticData
{
    public static string SomeValue { get; set; }
}
  1. 在ShellPage.xaml.cs中赋值:
AppStaticData.SomeValue = "Some text...";
  1. 在Page2.xaml中绑定(需添加命名空间引用):
<TextBlock Text="{Binding Source={x:Static local:AppStaticData.SomeValue}}" 
           Style="{ThemeResource SubtitleTextBlockStyle}" />

注:这种方式不会自动刷新UI,若需要更新后同步,需手动调用Page2的绑定刷新逻辑


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.18 11:55:16