0

How do I get the text bound to txtMessage from the second view model? When I had only one view model, the text was working fine. It does not work anymore when I moved the actual download code to second view model. Am I missing something? Any help appreciated.

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

CodeBehind:

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

viewModel:

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)); } } } 
2
  • Not related to your question, but this is hardly how MVVM should look like. You should use commands in your viewmodel instead of event handlers in your view. I recommend having a look at Josh Smith's excellent article: msdn.microsoft.com/en-us/magazine/dd419663.aspx Commented Oct 15, 2012 at 20:25
  • I have put this as a sample, if you are talking about RoutedCommnands. Yes, I use them but did not put this in the sample. I hope you are not talking about multiple view models serving one view. Commented Oct 15, 2012 at 20:34

1 Answer 1

2

Your viewModel1 has to be a property, and it's a field at the moment. Change it to:

public DownloadViewModel viewModel1 { get; set; } 

Explanation why such restriction exists, can be found here (primarily due to notification/verifications mechanisms simply not working for fields):

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.