我正在尝试创建一个新窗口并在其上显示完成情况。我使用BackgroundWorker报告事件来做到这一点。并且它更新了绑定到ProgressBar和TextBox的窗口的本地属性。
当进度更改
时会触发此操作 void copyBw_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
//This is public method of child window which sets progress and text properties
cpw.ChangeCompletion(e.ProgressPercentage, UserDefinedFileName);
}
这是我用来设置属性
的方法public void ChangeCompletion(int Value, string file){
Completion = Value;
FileCopiedString += file;
}
以下是我的XAML
<ProgressBar Name="CopyProgress" Margin="0,41,-2,324" Width="634" Height="5" Maximum="100" Value="{Binding Completion, RelativeSource={RelativeSource FindAncestor, AncestorType=Window}}" Grid.ColumnSpan="2"/>
<Label Content="Please wait while files are getting copied ... " Margin="10,10,384,334" Grid.ColumnSpan="2"/>
<RichTextBox Name="CopyFileList" Margin="10,62,10,10" Grid.ColumnSpan="2">
<FlowDocument>
<Paragraph>
<Run Text="{Binding FileCopiedString, RelativeSource={RelativeSource FindAncestor, AncestorType=Window}}"/>
</Paragraph>
</FlowDocument>
</RichTextBox>
两个属性的值都会更改,但不会向UI报告。
我检查了很多线程,但无法找到解决方案。对此的任何指示都会非常有用。
更新:当我直接设置ProgressBar的属性时,(没有绑定)然后它就可以了。
CopyProgress.Value = Value;
但这不对,绑定应该发生。现在我已将其缩小到Binding问题,但我不知道错误在哪里。
答案 0 :(得分:0)
我刚尝试过。它对我有用。 XAML:
private void testM() throws Exception {
pooler = DBPool_SF.getInstance();
dataSource = pooler.getDataSource();
Connection con = dataSource.getConnection();
con.setAutoCommit(false);
PreparedStatement ps,ps1 = null;
ResultSet rs = null;
String SEL_QUERY = "select item_id from test";
String UPDATE_QUERY = "insert into test_t values(?)";
ps = con.prepareStatement(SEL_QUERY);
rs = ps.executeQuery();
ps1 = con.prepareStatement(UPDATE_QUERY );
while (rs.next()) {
String id = rs.getString("item_id");
System.out.println(id);
ps1.setString(1,id);
ps1.addBatch();
}
ps1.executeBatch();
con.close();
}
C#:
<ProgressBar Name="CopyProgress" Height="15" Maximum="10" Value="{Binding Completion}" Margin="0,26,0,220" />
<Label Content="Please wait while files are getting copied ... " Margin="10,10,384,334" Grid.ColumnSpan="2"/>
<RichTextBox Name="CopyFileList" Margin="12,60,41,12">
<FlowDocument>
<Paragraph>
<Run Text="{Binding FileCopiedString}"/>
</Paragraph>
</FlowDocument>
</RichTextBox>
答案 1 :(得分:0)
最后,我已经能够解决它了。我认为它有点矫枉过正但它有效。
我没有关注MVVM。在我创建ViewModel并将其设置为View的默认DataContext之后,它工作正常。当我试图与ViewModel和View混淆时,虽然它应该可以工作,但事实并非如此。
查看:
private void InitializeQueue()
{
try
{
_mq = MessageQueue.GetPrivateQueuesByMachine(_queueServerName).Where(qu => qu.Path == _queueAddress).FirstOrDefault();
_mq.Formatter = new BinaryMessageFormatter();
_mq.ReceiveCompleted += MessageReceiveCompleted;
}
catch (Exception ex)
{
Trace.WriteLine("Failed to initialize Queue!" + Environment.NewLine + " Error:" + Environment.NewLine + ex.Message);
throw;
}
}
async void MessageReceiveCompleted(object sender, ReceiveCompletedEventArgs e)
{
_mq.BeginReceive();
try
{
await ProcessMessageAsync(e.Message);
}
catch (Exception ex)
{
Trace.WriteLine("Error occured during report fetching:" + Environment.NewLine + ex.Message);
throw;
}
}
视图模型:
enter public partial class Window1 : Window, INotifyPropertyChanged{
public Window1(){
ProgressBarViewModel pbvm = new ProgressBarViewModel();
InitializeComponent();
DataContext = pbvm;
}}
}