2012-10-15 61 views
0

如何從第二個視圖模型中獲取綁定到txtMessage的文本?當我只有一個視圖模型時,文本工作正常。當我將實際的下載代碼移到第二個視圖模型時,它不再起作用。我錯過了什麼嗎?任何幫助讚賞。WPF綁定到第二個ViewModel中的屬性

的Xaml:

<DockPanel DockPanel.Dock="Top"> 
    <TextBlock x:Name="txtMessage" DockPanel.Dock="Top" Margin="5" Text="{Binding viewModel1.Message}" /> 
    <StackPanel DockPanel.Dock="Top" Orientation="Horizontal" Margin="5,5"> 
    <ProgressBar Width="300" Visibility="{Binding IsDownloading, Converter={converter:VisibilityConverter}}" IsIndeterminate="True" /> 
<Button Content="Cancel" /> 
</StackPanel> 
</DockPanel> 
<Button Content="Download" Width="120" Margin="0,0,5,0" Name="btnSubmit" Click="btnSubmit_Click" /> 

代碼隱藏:

public partial class DownloadWindow: Window 
    { 
     DownloadWindowViewModel viewModel = new DownloadWindowViewModel(); 

     public DownloadWindow() 
     { 
      InitializeComponent(); 
      this.DataContext = viewModel; 
     } 

     private void btnSubmit_Click(object sender, RoutedEventArgs e) 
     { 
      viewModel.IsDownloading = true; 
      viewModel.Download(); 

     } 

    } 

視圖模型:

public class DownloadWindowViewModel: INotifyPropertyChanged 
    { 

     Thread downloadThread; 
     public DownloadViewModel viewModel1; 

     public DownloadWindowViewModel() 
     { 
      viewModel1 = new DownloadViewModel(); 
     } 

     private bool _isDownloading; = false; 

     public bool IsDownloading 
     { 
      get 
      { 
       return _isDownloading; 
      } 
      set 
      { 
       _isDownloading; = value; 
       OnPropertyChanged("IsDownloading"); 
      } 
     } 

     public void Download() 
     { 
      viewModel1.Download(); 

     } 

     public event PropertyChangedEventHandler PropertyChanged; 

     public void OnPropertyChanged(string propertyName) 
     { 
      PropertyChangedEventHandler handler = PropertyChanged; 
      if (handler != null) 
      { 
       handler(this, new PropertyChangedEventArgs(propertyName)); 
      } 
     } 


    } 

viewModel1:

public class DownloadViewModel: INotifyPropertyChanged 
    { 
     Thread _thread; 

     public void Download() 
     { 
      ThreadStart threadStart = delegate() 
      { 
       StartDownload(); 
      }; 
      _thread = new Thread(threadStart); 
      _thread.IsBackground = true; 
      _thread.Start(); 

     } 

     private void StartDownload() 
     { 
      for (int i = 10; i < 1500; i++) 
      { 
       Thread.Sleep(5000); 
       Message = "Downloading " + i.ToString(); 
      } 
     } 

     private string _message = ""; 

     public string Message 
     { 
      get 
      { 
       return _message; 
      } 
      set 
      { 
       if (_message != value) 
       { 
        _message = value; 
        OnPropertyChanged("Message"); 
       } 

      } 
     } 

     public event PropertyChangedEventHandler PropertyChanged; 

     protected void OnPropertyChanged(string propertyName) 
     { 
      PropertyChangedEventHandler handler = PropertyChanged; 
      if (handler != null) 
      { 
       handler(this, new PropertyChangedEventArgs(propertyName)); 
      } 
     } 

    } 
+0

還沒有相關的到你的問題,但這幾乎不像MVVM應該如何。您應該在視圖模型中使用命令,而不是視圖中的事件處理程序。我建議看看Josh Smith的優秀文章:http://msdn.microsoft.com/en-us/magazine/dd419663.aspx –

+0

如果你正在談論RoutedCommnands,我已經把它作爲示例。是的,我使用它們,但沒有把它放在樣本中。我希望你不是在討論一種觀點的多視圖模型。 – isakavis

回答

相關問題