【发布时间】:2016-09-29 11:42:23
【问题描述】:
我正在尝试创建一个新窗口并在其上显示完成。我正在使用 BackgroundWorker 报告事件来做到这一点。并更新绑定到 ProgressBar 和 TextBox 的 window 的本地属性。
当进度改变时触发
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;
但这是不对的,应该发生绑定。现在我把它缩小到绑定问题,但我不知道错误在哪里。
【问题讨论】:
-
您是否尝试将
UpdateSourceTrigger属性从绑定设置为PropertyChanged? -
输出窗口是否有绑定错误?
-
你设置DataContext了吗?
-
•否 输出窗口中没有绑定错误。 • 我刚刚尝试了UpdateSourceTrigger,但它也不起作用。 • 我在它的构造函数中将DataContext 设置为子窗口
this.DataContext = this; -
更改进度条边距..