我是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中的文本块,其中包含&#34; Reading Lines 300/3000&#34;以及进度条指示在将数据传输到列表视图时将其值更新为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();
}
}
提前致谢!我希望有人可以帮助我:)。
答案 0 :(得分:0)
您可以使用后台工作人员报告进度回调更新属性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));
};
答案 1 :(得分:-1)
也许这可以帮助您使用ProgressBar:
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.Text
后调用TextBloxName.Refresh