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

时间:2016-07-20 09:21:39

标签: c# wpf mvvm inotifypropertychanged textblock

我的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>

1 个答案:

答案 0 :(得分:0)

您需要在后台线程中运行以更新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));
    }
}