【问题标题】:How to know the textbox finish to read all the file如何知道文本框完成读取所有文件
【发布时间】:2016-05-27 10:03:30
【问题描述】:

我有一个 WPF 应用程序,它读取一个巨大的文本文件并将其显示到文本框,但我需要知道在将规范化按钮更改为可见之前已读取所有文件。

我的问题是我如何知道所有文件都已读取并显示在文本框中(意味着文本框仍在更改)并且在将按钮分配为可见之后?

        try
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            if (openFileDialog.ShowDialog() == true)
            {
                using (StreamReader sr = new StreamReader(openFileDialog.FileName))
                {
                    FileBuff = await sr.ReadToEndAsync();
                    txtEditor.Text = FileBuff;
                    Normalize_button.IsEnabled = true;
                }             
            }
        }
        catch (Exception ex)
        {
            UiInvoke(() => txtEditor.Text = "Could not read the file");
        }

【问题讨论】:

  • 您的意思是要等待文本实际加载到文本框中吗?您尝试过TextChanged 事件吗?
  • 是的,我的意思是……我读到了“TextChanged”,但我无法使用它。
  • 我建议你使用BackgroundWorker。您可以使用 OnCompleted 事件
  • 山姆,是的,我现在试过了,但它没有将按钮更改为可见:(
  • 按钮启用或可见?

标签: c# wpf textbox fileopendialog


【解决方案1】:

在 XAML 中绑定 BigText
我也会绑定按钮 IsEnabled

string bigText 
string BigText  
{
    get { return bigText; }
    set 
    {
        bigText = value;
        Normalize_button.IsEnabled = true;
        NotifyPropertyChanged("BigText");
    }
}

FileBuff = sr.ReadToEndAsync();
BigText = await FileBuff;

但您应该使用后台工作程序,以便您有一个回调事件 (OnCompleted)
您还可以选择取消后台工作人员
查看优先级绑定

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-14
    • 2010-10-08
    • 2015-07-13
    • 1970-01-01
    • 2011-05-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多