简体   繁体   English

更新另一个窗口的进度条

[英]Updating progress bar of another window

I'm trying to create a new window and show completion on it. 我正在尝试创建一个新窗口并在其上显示完成。 I'm using BackgroundWorker report event to do that. 我正在使用BackgroundWorker报告事件来做到这一点。 And it updates local properties of window which are bound to ProgressBar and TextBox. 并更新绑定到ProgressBar和TextBox的window的本地属性。

This is triggered when progress Changes 当进度更改时触发

    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);
    }

This is the method i'm using to set the properties 这是我用来设置属性的方法

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

Below is my XAML 以下是我的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>

The value of both properties get changed but won't report it to UI. 这两个属性的值都会更改,但不会向UI报告。

I've checked many threads but couldn't find the solution. 我检查了许多线程,但找不到解决方案。 Any pointers on this would be really helpful. 任何有关此的指示将非常有帮助。

Update: When I set the property of ProgressBar Directly, (Without binding) then it works. 更新:当我直接设置ProgressBar的属性时(无绑定),它将起作用。 CopyProgress.Value = Value;

But this is not right and binding should happen. 但这是不对的,应该发生约束力。 Now I've narrowed it down to Binding problem but I don't know where the mistake is. 现在,我将其范围缩小到绑定问题,但是我不知道错误在哪里。

I just tried it. 我刚试过 it is working for me. 它为我工作。 XAML : 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#: 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));
        }
    }
}

Finally, I've been able to resolve it. 最后,我已经能够解决它。 I think its a bit overkill but it worked. 我认为这有点矫kill过正,但确实有效。

I wasn't following MVVM. 我没有关注MVVM。 After I created a ViewModel and set that as default DataContext to View, it worked. 创建ViewModel并将其设置为默认的DataContext后,它就可以工作了。 When I tried to mix up with the ViewModel and View, eventhough it is supposed to work, it didn't. 当我尝试将ViewModel和View混合使用时,尽管它应该可以工作,但却没有。

View: 视图:

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

ViewModel: ViewModel:

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));
    }
}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM