WPF C# 数据绑定 - MessageBox.Show
WPF C# Databinding - MessageBox.Show
让我首先声明我已经阅读了很多我头疼的线程,我已经尝试了其中的大部分但仍然没有成功。
我的问题很简单。我有一些状态要更新控件,我有一个绑定到对象 class 的文本框。
当我逐步执行代码时,textbox.Text 正在更新,但从未呈现。我注意到的是,如果我使用我熟悉的其他以 windows 为中心的东西,例如 MessageBox.Show();在我的循环内部,它中断了 MainWindow 并更新了文本框..!
MessageBox.Show() 中究竟是什么 allows/forces/coerces 渲染发生了,我不能直接调用它吗?我处理这个问题的方式似乎是最简单的版本,当你需要数据绑定才能开箱即用而没有任何繁琐的工作时..希望我错过了简单的按钮..
任何帮助都将非常有价值。提前感谢您的宝贵时间。
public partial class MainWindow : Window{
private ExampleDataClass objClass = null;
public MainWindow()
{
InitializeComponent();
objClass = new ExampleDataClass();
this.DataContext = objClass;
}
public int j = 0;
private string _someLocalString = "";
public string SomeLocalString
{
get { return _someLocalString; }
set { _someLocalString = value; }
}
public string strstr = "";
private void textBox_TextChanged(object sender, TextChangedEventArgs e)
{
//MessageBox.Show(""); // this will make it work..
}
private void button_Click(object sender, RoutedEventArgs e)
{
SomeLongRunningCode(out strstr);
}
public void SomeLongRunningCode(out string SomeProperty)
{
for (int i = 1; i <= 600; i++)
{
SomeLocalString = i.ToString();
objClass.X = _someLocalString;
// BindingOperations.GetBindingExpressionBase(textBox, TextBox.TextProperty).UpdateTarget();
// textBox.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal,
// new Action(delegate() { textBox.Text = i.ToString();}));
}
SomeProperty = "Exited SomeLongRunningCode";
}}}
<Window x:Class="DataBinding.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:DataBinding"
mc:Ignorable="d"
Title="MainWindow" Height="109.157" Width="176.303">
<Grid x:Name="myGrid">
<TextBox x:Name="textBox" Text="{Binding X,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Focusable = "true" HorizontalAlignment="Left" Height="23" Margin="22,14,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" TextChanged="textBox_TextChanged" />
<Button x:Name="button" Content="Button" HorizontalAlignment="Left" Margin="24,46,0,0" VerticalAlignment="Top" Width="75" Click="button_Click"/>
</Grid>
public class ExampleDataClass : INotifyPropertyChanged
{
private string _x = "";
public string X
{
get { return _x;}
set
{
if (_x != null)
{
_x = value;
OnPropertyChanged("X");
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string info)
{
var handler = this.PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(info));
}
}
}}
textBox 未更新,因为线程在 for 循环期间被阻塞。
如果您 运行 SomeLongRunningCode
异步,您将看到您的代码按预期工作。
private void button_Click(object sender, RoutedEventArgs e)
{
Task.Run(() => SomeLongRunningCode(out strstr));
}
public void SomeLongRunningCode(out string SomeProperty)
{
for (int i = 1; i <= 600; i++)
{
SomeLocalString = i.ToString();
objClass.X = _someLocalString;
Thread.Sleep(100); // Slowing down so we can see the text change
}
SomeProperty = "Exited SomeLongRunningCode";
}
让我首先声明我已经阅读了很多我头疼的线程,我已经尝试了其中的大部分但仍然没有成功。
我的问题很简单。我有一些状态要更新控件,我有一个绑定到对象 class 的文本框。
当我逐步执行代码时,textbox.Text 正在更新,但从未呈现。我注意到的是,如果我使用我熟悉的其他以 windows 为中心的东西,例如 MessageBox.Show();在我的循环内部,它中断了 MainWindow 并更新了文本框..!
MessageBox.Show() 中究竟是什么 allows/forces/coerces 渲染发生了,我不能直接调用它吗?我处理这个问题的方式似乎是最简单的版本,当你需要数据绑定才能开箱即用而没有任何繁琐的工作时..希望我错过了简单的按钮..
任何帮助都将非常有价值。提前感谢您的宝贵时间。
public partial class MainWindow : Window{
private ExampleDataClass objClass = null;
public MainWindow()
{
InitializeComponent();
objClass = new ExampleDataClass();
this.DataContext = objClass;
}
public int j = 0;
private string _someLocalString = "";
public string SomeLocalString
{
get { return _someLocalString; }
set { _someLocalString = value; }
}
public string strstr = "";
private void textBox_TextChanged(object sender, TextChangedEventArgs e)
{
//MessageBox.Show(""); // this will make it work..
}
private void button_Click(object sender, RoutedEventArgs e)
{
SomeLongRunningCode(out strstr);
}
public void SomeLongRunningCode(out string SomeProperty)
{
for (int i = 1; i <= 600; i++)
{
SomeLocalString = i.ToString();
objClass.X = _someLocalString;
// BindingOperations.GetBindingExpressionBase(textBox, TextBox.TextProperty).UpdateTarget();
// textBox.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal,
// new Action(delegate() { textBox.Text = i.ToString();}));
}
SomeProperty = "Exited SomeLongRunningCode";
}}}
<Window x:Class="DataBinding.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:DataBinding"
mc:Ignorable="d"
Title="MainWindow" Height="109.157" Width="176.303">
<Grid x:Name="myGrid">
<TextBox x:Name="textBox" Text="{Binding X,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Focusable = "true" HorizontalAlignment="Left" Height="23" Margin="22,14,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" TextChanged="textBox_TextChanged" />
<Button x:Name="button" Content="Button" HorizontalAlignment="Left" Margin="24,46,0,0" VerticalAlignment="Top" Width="75" Click="button_Click"/>
</Grid>
public class ExampleDataClass : INotifyPropertyChanged
{
private string _x = "";
public string X
{
get { return _x;}
set
{
if (_x != null)
{
_x = value;
OnPropertyChanged("X");
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string info)
{
var handler = this.PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(info));
}
}
}}
textBox 未更新,因为线程在 for 循环期间被阻塞。
如果您 运行 SomeLongRunningCode
异步,您将看到您的代码按预期工作。
private void button_Click(object sender, RoutedEventArgs e)
{
Task.Run(() => SomeLongRunningCode(out strstr));
}
public void SomeLongRunningCode(out string SomeProperty)
{
for (int i = 1; i <= 600; i++)
{
SomeLocalString = i.ToString();
objClass.X = _someLocalString;
Thread.Sleep(100); // Slowing down so we can see the text change
}
SomeProperty = "Exited SomeLongRunningCode";
}