Updating value of TextBlock in wpf dynamically [Sovled]

坚强是说给别人听的谎言 提交于 2019-12-10 12:25:01

问题


I have text block on my UI. I would like to display some text on the text block dynamically. I have implemented it as given in the below code. however i do not see the values updating dynamically. I do see only the last updated value on UI text block. I have included a delay to notice the change.

Please provide any solution or comment for more info.Thank you in advance.

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>

回答1:


You need to Run in Background thread to update your values in UI TextBlock

Code:

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));
    }
}


来源:https://stackoverflow.com/questions/38477276/updating-value-of-textblock-in-wpf-dynamically-sovled

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!