2016-09-29 68 views
0

我正試圖創建一個新窗口並顯示完成。我正在使用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;

但是這是不正確的,應該發生約束。現在我已經把它縮小到綁定問題,但我不知道錯誤在哪裏。

+1

您是否嘗試設置['UpdateSourceTrigger'](https://msdn.microsoft.com/de-de /library/system.windows.data.updatesourcetrigger(v=vs.110).aspx)綁定到'PropertyChanged'的屬性? –

+1

輸出窗口中是否存在綁定錯誤? – Gusdor

+0

你設置了DataContext嗎? –

回答

0

最後,我已經能夠解決這個問題。我認爲它有點矯枉過正,但它工作。

我沒有關注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)); 
    } 
} 

}

0

我剛試過。它爲我工作。 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)); 
     } 
    } 
} 
+0

我複製粘貼相同的東西。即使在那之後它不起作用。 – Prajwal