在wpf中动态更新TextBlock的值[已解决] [英] Updating value of TextBlock in wpf dynamically [Sovled]
本文介绍了在wpf中动态更新TextBlock的值[已解决]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的UI上有文本块.我想在文本块上动态显示一些文本.我已经按照下面的代码实现了它.但是我看不到值动态更新.我确实只在UI文本块上看到了最后更新的值.我已经延迟通知了更改.
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>
推荐答案
您需要在后台运行线程以更新UI TextBlock中的值
You need to Run in Background thread to update your values in 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));
}
}
这篇关于在wpf中动态更新TextBlock的值[已解决]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文