簡體   English   中英

MVVM WPF中的數據綁定

[英]Data Binding in MVVM WPF

我對MVVM模式知之甚少。 我的問題是數據沒有綁定到xaml控件,那里沒有錯誤。 進度欄仍為0。

主頁視圖模型

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;
using CSMS_MVVM.Models;

namespace CSMS_MVVM.ViewModels
{
    class HomepageViewModel : INotifyPropertyChanged
    {
        BackgroundWorker _worker;
        public int _progress=20;
        public int Progress
        {
            get { return _progress; }
            set {
                _progress = value;
                OnPropertyChanged(new PropertyChangedEventArgs("Progress"));
            }
        }
        public void startBackgroundProcess()
        {
            _worker = new BackgroundWorker();
            _worker.DoWork += new DoWorkEventHandler(worker_DoWork);
            _worker.ProgressChanged += worker_Progress_Changed;
            _worker.RunWorkerAsync();
        }

        private void worker_Progress_Changed(object sender, ProgressChangedEventArgs e)
        {
            Progress = e.ProgressPercentage;
        }

        private void worker_DoWork(object sender, DoWorkEventArgs e)
        {
            Progress = 20;
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;
        public void OnPropertyChanged(PropertyChangedEventArgs e)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, e);
        }

        #endregion
    }
}

Homepage.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using CSMS_MVVM.ViewModels;

namespace CSMS_MVVM.Views
{
    /// <summary>
    /// Interaction logic for Homepage.xaml
    /// </summary>
    public partial class Homepage : Page
    {
        HomepageViewModel hvm;
        public Homepage()
        {
            InitializeComponent();
        }

        private void Page_Loaded_1(object sender, RoutedEventArgs e)
        {
            hvm = new HomepageViewModel();
            hvm.startBackgroundProcess();
        }
    }
}

Homepage.xaml

<Page x:Class="CSMS_MVVM.Views.Homepage"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      xmlns:local="clr-namespace:CSMS_MVVM.ViewModels"
      mc:Ignorable="d" 
      d:DesignHeight="700" d:DesignWidth="1000"
    Title="Homepage" Loaded="Page_Loaded_1" Name="main">
<ProgressBar Name="pbStatus" Value="{Binding Path=Progress}" HorizontalAlignment="Center" Height="20" Margin="0,582,0,0" VerticalAlignment="Top" Width="300">
-----
            <ProgressBar.Effect>
                <DropShadowEffect Color="#FFB6B6B6" ShadowDepth="3" BlurRadius="15" Direction="310"/>
            </ProgressBar.Effect>
        </ProgressBar>
        <TextBlock Text="{Binding Progress}" Margin="0,582,0,0" HorizontalAlignment="Center" VerticalAlignment="Top" />
        <Label Content="Loading ..." HorizontalAlignment="Center" Margin="0,607,0,0" VerticalAlignment="Top"/>
------
</Page>

我在做正確的編碼嗎? 我在互聯網上搜索了一個示例,但我不理解它們。

謝謝!

如果未指定其他任何內容,則您設置的所有綁定都將綁定到DataContext

private void Page_Loaded_1(object sender, RoutedEventArgs e)
{
    hvm = new HomepageViewModel();
    hvm.startBackgroundProcess();

    this.DataContext = hvm;
}

將數據上下文設置為viewmodel實例后,它應該可以工作。

您可以在后面的代碼中設置頁面的DataContext。

另一個選擇是將ViewModel創建為XAML資源,並通過數據綁定設置子元素的數據上下文:

<Page x:Class="CSMS_MVVM.Views.Homepage"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
  xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
  xmlns:local="clr-namespace:CSMS_MVVM.ViewModels"
  mc:Ignorable="d" 
  d:DesignHeight="700" d:DesignWidth="1000"
Title="Homepage" Loaded="Page_Loaded_1" Name="main">
  <Page.Resources>
        <!-- This creates the instance of the HomepageViewModel -->
        <local:HomepageViewModel x:Key="HomepageViewModel" />
  </Page.Resources>
  <ProgressBar DataContext="{StaticResource HomepageViewModel}" 
        Value="{Binding Path=Progress}">
public Homepage()       
{        
   InitializeComponent();      
   hvm = new HomepageViewModel();
   this.DataContext=hvm;      
}

在構造函數內創建視圖模型對象。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM