0

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

xml:

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


    }

视图模型1:

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

    }
4

1 回答 1

2

viewModel1必须是一个属性,并且它现在是一个字段。将其更改为:

public DownloadViewModel viewModel1 { get; set; }

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

于 2012-10-15T20:22:53.873 回答