动态更新wpf中TextBlock的值[Sovled]

本文关键字:Sovled 的值 TextBlock 更新 wpf 动态 | 更新日期: 2023-09-27 17:57:44

我的UI上有文本块。我想在文本块上动态显示一些文本。我已经按照下面的代码实现了它。然而,我没有看到这些值在动态更新。我只在UI文本块上看到最后更新的值。我已经包括了一个延迟通知的变化。

请提供任何解决方案或评论以了解更多信息。提前谢谢。

Code:
namespace TxtBlock
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        SomeObjectClass obj = new SomeObjectClass();
        public MainWindow()
        {
            InitializeComponent();
            txtName.DataContext = obj;
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            obj.Name = "Hello World";
             Thread.Sleep(2000);
           obj.Name = "Third";
        }
    }
    class SomeObjectClass : INotifyPropertyChanged
    {
        private string _name = "hello";
        public string Name
        {
            get
            {
                return _name;
            }
            set
            {
                _name = value;
                OnPropertyChanged("Name");
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;
        public void OnPropertyChanged(string PropertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(PropertyName));
        }
    }
}
XAML: <Window x:Class="TxtBlock.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">
    <Grid>
        <Button Content="Button" HorizontalAlignment="Left" Margin="237,170,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>
        <TextBlock HorizontalAlignment="Left" Margin="237,256,0,0" TextWrapping="Wrap"  x:Name="txtName" Text="{Binding Name}" VerticalAlignment="Top"/>
    </Grid>
</Window>

动态更新wpf中TextBlock的值[Sovled]

您需要在后台线程中运行以更新UI TextBlock 中的值

代码:

public partial class TextBlockExample : Window
{
    ThreadExampleViewModel viewModel = new ThreadExampleViewModel();
    public TextBlockExample()
    {
        InitializeComponent();
        this.DataContext = viewModel;
    }
    private void btnClick_Click(object sender, RoutedEventArgs e)
    {
        /// Background thread Thread to run your logic 
        Thread thread = new Thread(YourLogicToUpdateTextBlock);
        thread.IsBackground = true;
        thread.Start();
    }
    private void YourLogicToUpdateTextBlock()
    {
        /// Example i am updating with i value.
        for (int i = 0; i < 1000; i++)
        {
            viewModel.Name = i + " Conut";
            Thread.Sleep(1000);
        }
    }
}

<Grid>
    <StackPanel>
        <TextBlock x:Name="txtName" Text="{Binding Name}" Height="30" Width="100" Margin="10"/>
        <Button x:Name="btnClick" Content="Click" Height="30" Width="100" Margin="10" Click="btnClick_Click"/>
    </StackPanel>
</Grid>


public class ThreadExampleViewModel : INotifyPropertyChanged
{
    private string name = "Hello";
    public string Name
    {
        get { return name; }
        set { name = value; OnPropertyChanged("Name"); }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    public void OnPropertyChanged(string PropertyName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(PropertyName));
    }
}