【问题标题】:Updating progress bar of another window更新另一个窗口的进度条
【发布时间】: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;
  • 更改进度条边距..

标签: c# wpf


【解决方案1】:

我刚试过。它对我有用。 XAML:

<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>

c#:

 public partial class Window1 : Window, INotifyPropertyChanged
{
    public Window1()
    {
        InitializeComponent();
        DataContext = this;
    }

    int _completion;
    public int Completion
    {
        get { return _completion; }
        set
        {
            _completion = value;
            Notify("Completion");
        }
    }

    private string _fileCopied;
    public string FileCopiedString
    {
        get { return _fileCopied; }
        set
        {
            _fileCopied = value;
            Notify("FileCopiedString");
        }
    }

    public void ChangeCompletion(int Value, string file)
    {
        Completion = Value;
        FileCopiedString = FileCopiedString + file;
    }

    public event PropertyChangedEventHandler PropertyChanged;

    public void Notify(string name)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(name));
        }
    }
}

【讨论】:

  • 我复制粘贴了同样的东西。即使在那之后它也不起作用。
【解决方案2】:

最后,我已经能够解决它。我认为这有点矫枉过正,但确实有效。

我没有关注 MVVM。在我创建了一个 ViewModel 并将其设置为 View 的默认 DataContext 之后,它就起作用了。当我尝试将 ViewModel 和 View 混合使用时,尽管它应该可以工作,但它没有。

查看:

enter public partial class Window1 : Window, INotifyPropertyChanged{
public Window1(){   
ProgressBarViewModel pbvm = new ProgressBarViewModel();
    InitializeComponent();
    DataContext = pbvm;
}}

视图模型:

public partial class ProgressBarViewModel : INotifyPropertyChanged{
int _completion;
    public int Completion
    {
        get { return _completion; }
        set
        {
            _completion = value;
            Notify("Completion");
        }
    }

private string _fileCopied;
public string FileCopiedString
{
    get { return _fileCopied; }
    set
    {
        _fileCopied = value;
        Notify("FileCopiedString");
    }
}

public void ChangeCompletion(int Value, string file)
{
    Completion = Value;
    FileCopiedString = FileCopiedString + file;
}

public event PropertyChangedEventHandler PropertyChanged;

public void Notify(string name)
{
    if (PropertyChanged != null)
    {
        PropertyChanged(this, new PropertyChangedEventArgs(name));
    }
}

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多