如何在wpf中更新进度条和文本块的值

本文关键字:文本 wpf 更新 | 更新日期: 2023-09-27 18:11:19

我是wpf和c#的新手,也在这里问。我在网上搜索了几个小时,我还没有找到解决我问题的方法。我有一个进度条,每当循环增加时更新它的值,一个文本块更新它的文本(例如:阅读行5/3000)。目前,我没有绑定路径,所以我在xaml中创建了一个虚拟绑定。我不知道该怎么做。

这是我的XAML:
<xctk:BusyIndicator x:Name="_busyIndicator" Grid.Row="4">
                <xctk:BusyIndicator.BusyContentTemplate>
                    <DataTemplate>
                        <StackPanel Margin="4">
                            <TextBlock Text="Parsing OTM File" FontWeight="Bold" HorizontalAlignment="Center" />
                            <StackPanel Margin="4">
                                <TextBlock Text="{Binding NumberOfLinesOfStringInTextFile}" />
                                <ProgressBar Grid.Row="4" Name="prgParse" Height="15" Value="{Binding CurrentLineNumberAddedToListView}" />
                            </StackPanel>
                        </StackPanel>
                    </DataTemplate>
                </xctk:BusyIndicator.BusyContentTemplate>
                <xctk:BusyIndicator.OverlayStyle>
                    <Style TargetType="Rectangle">
                        <Setter Property="Fill" Value="#ffffeeee"/>
                    </Style>
                </xctk:BusyIndicator.OverlayStyle>
                <xctk:BusyIndicator.ProgressBarStyle>
                    <Style TargetType="ProgressBar">
                        <Setter Property="Visibility" Value="Collapsed"/>
                    </Style>
                </xctk:BusyIndicator.ProgressBarStyle>
                <ListBox x:Name="_listBox" />
            </xctk:BusyIndicator>

我有一个按钮,触发读取文本文件内的行循环(在这种情况下,它有3000+行),并将字符串行放置到listview。我想在我的xaml中更新文本块,声明"阅读行300/3000"和进度条,指示它的百分比,更新其值为10%或10%,而它正在将数据传输到列表视图。

下面是我的代码:
 public partial class MainWindow : Window, INotifyPropertyChanged
    {
        public MainWindow()
        {
            InitializeComponent();
            DataContext = this;
        }
        private void cmdBrowse_Click(object sender, RoutedEventArgs e)
        {
            var OpenOTM = new Microsoft.Win32.OpenFileDialog();
            OpenOTM.DefaultExt = ".otm";
            OpenOTM.Filter = "OTM Files (*.otm)|*.otm|TEXT Files (*.txt)|*.txt"; 
            if (OpenOTM.ShowDialog().GetValueOrDefault(false))
            {
                txtFilePath.Text = OpenOTM.FileName;
            }
        }
        private void cmdOk_Click(object sender, RoutedEventArgs e)
        {
            WithAdjustments wa = new WithAdjustments();
            BackgroundWorker worker = new BackgroundWorker();
            string[] lines = System.IO.File.ReadAllLines(txtFilePath.Text);
            worker.DoWork += (o, ea) =>
            {
                List<String> listOfString = new List<string>();
                foreach (string lin in lines)
                {
                    listOfString.Add(lin);
                    Thread.Sleep(2);
                }
                Dispatcher.Invoke((Action)(() => _listBox.ItemsSource = listOfString));
            };
            worker.RunWorkerCompleted += (o, ea) =>
            {
                _busyIndicator.IsBusy = false;
            };
            _busyIndicator.IsBusy = true;
            worker.RunWorkerAsync();
        }
    }

提前感谢!我希望有人能帮助我:)

如何在wpf中更新进度条和文本块的值

你可以更新你的属性CurrentLineNumberAddedToListView, NumberOfLinesOfStringInTextFile使用后台工作报告进度回调,

worker.WorkerSupportsCancellation = true;
worker.WorkerReportsProgress = true;
worker.ProgressChanged += (o, e) =>
{
     //Here in e.ProgressPercentage you will get the current line number read you can deduct the textblock text and progressbar value to be set on CurrentLineNumberAddedToListView and NumberOfLinesOfStringInTextFile
}
worker.DoWork += (o, ea) =>
        {
            List<String> listOfString = new List<string>();
            BackgroundWorker worker = o as BackgroundWorker;
            int numberofLine = 0;
            foreach (string lin in lines)
            {
                listOfString.Add(lin);
                o.ReportProgress(++numberofLine);
                Thread.Sleep(2);
            }

            Dispatcher.Invoke((Action)(() => _listBox.ItemsSource = listOfString));
        };

也许这可以帮助你的进度条:

Private Sub InitProgressBar(ByVal DblMaximumValueI As Double)
    'set min and max
    ProgressBar1.Minimum = 0
    ProgressBar1.Maximum = DblMaximumValueI
    'set start value
    ProgressBar1.Value = 0
End Sub
Private Sub IncremenetProgressBar_()
    ProgressBar1.Value += 1 'or variable
End Sub

并调用下面的sub来显示进度:

Public Sub IncrementProgressBar()
   Application.Current.Dispatcher.Invoke(DispatcherPriority.Background, New Action(AddressOf IncremenetProgressBar_))
End Sub

和更新你的TextBlock你可以添加一个模块:

Module ExtensionMethods
'anonymous delegates are not allowed in VB.NET, so we need an empty method
Private Sub EmptyMethod()
End Sub
'refresh for Controls
<Extension()> _
Public Sub Refresh(ByVal uiElement As UIElement)
    uiElement.Dispatcher.Invoke(DispatcherPriority.Render, New Action(AddressOf EmptyMethod))
End Sub
End Module

然后调用TextBloxName。设置TextBloxName后刷新。文本