WPF - 运行时更新绑定问题

发布于 2024-09-14 12:14:11 字数 1843 浏览 7 评论 0原文

我是 C# 的新手,也是 WPF 的新手。对于专业人士来说,这可能是一个非常基本的问题和小菜一碟。请耐心听我说。

我需要显示一个动态文本块,其中的文本在运行时发生更改,而无需额外的触发器(例如按钮单击等)。由于某种原因(显然我对这个概念的理解不够),文本块保持为空。

Xaml 尽可能简单:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
    <Grid>
        <TextBlock Text="{Binding Path=Name}"/>
    </Grid>
</Window>

并且其背后的代码也简化了:

using System.ComponentModel;
using System.Threading;
using System.Windows;

namespace WpfApplication1
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            Client client = new Client();
            client.Name = "Michael";
            Thread.Sleep(1000);
            client.Name = "Johnson";
        }
    }


    public class Client : INotifyPropertyChanged
    {
        private string name = "The name is:";
        public event PropertyChangedEventHandler PropertyChanged;

        public string Name
        {
            get
            {
                return this.name;
            }
            set
            {
                if (this.name == value)
                    return;

                this.name = value;
                this.OnPropertyChanged(new PropertyChangedEventArgs("Name"));
            }
        }

        protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)
        {
            if (this.PropertyChanged != null)
                this.PropertyChanged(this, e);
        }
    }
}

提前致谢,

Ceres

I'm quite a novice with C# and a complete newbie regarding WPF. Probably it's a very basic question and piece of cake for the pros. Please bear with me.

I need to display a dynamic textblock with text changed in run time without additional triggers such as button clicks and so. For some reason (my insufficient understanding of the concept obviously) textblock stays empty.

Xaml is as simple as it can be:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
    <Grid>
        <TextBlock Text="{Binding Path=Name}"/>
    </Grid>
</Window>

And the code behind it simplified as well:

using System.ComponentModel;
using System.Threading;
using System.Windows;

namespace WpfApplication1
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            Client client = new Client();
            client.Name = "Michael";
            Thread.Sleep(1000);
            client.Name = "Johnson";
        }
    }


    public class Client : INotifyPropertyChanged
    {
        private string name = "The name is:";
        public event PropertyChangedEventHandler PropertyChanged;

        public string Name
        {
            get
            {
                return this.name;
            }
            set
            {
                if (this.name == value)
                    return;

                this.name = value;
                this.OnPropertyChanged(new PropertyChangedEventArgs("Name"));
            }
        }

        protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)
        {
            if (this.PropertyChanged != null)
                this.PropertyChanged(this, e);
        }
    }
}

Thanks in advance,

Ceres

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

内心荒芜 2024-09-21 12:14:11

为了使绑定工作,您需要将窗口的 DataContext 设置为要绑定的对象,在本例中为客户端对象。

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    Client client = new Client();

    // Set client as the DataContext.
    DataContext = client;

    client.Name = "Michael";
    Thread.Sleep(1000);
    client.Name = "Johnson";
}

这应该会导致文本框成功更新。

只是指出,在加载事件中使用 Thread.Sleep() 会导致程序在启动时挂起一秒钟,更好的主意是使用 WPF DispatcherTimer 创建 1 秒延迟。

希望有帮助!

In order for the binding to work you need to set the DataContext of the window to the object you want to bind to, in this instance the client object.

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    Client client = new Client();

    // Set client as the DataContext.
    DataContext = client;

    client.Name = "Michael";
    Thread.Sleep(1000);
    client.Name = "Johnson";
}

This should cause the TextBox to successfully update.

Just to point out that using Thread.Sleep() in the loaded event causes the program to hang for a second on startup, a better idea would be to use the WPF DispatcherTimer to create the 1 second delay.

Hope that helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文