WPF绑定到第二个ViewModel中的属性

时间:2012-10-15 19:55:25

标签: c# wpf viewmodel

如何从第二个视图模型获取绑定到txtMessage的文本?当我只有一个视图模型时,文本工作正常。当我将实际下载代码移动到第二个视图模型时,它不再起作用了。我错过了什么吗?任何帮助表示赞赏。

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

    }

1 个答案:

答案 0 :(得分:2)

您的viewModel1必须是一个属性,而且此刻它是一个字段。将其更改为:

public DownloadViewModel viewModel1 { get; set; }

解释为什么存在这样的限制,可以在这里找到(主要是由于通知/验证机制根本不适用于字段):