2016-05-16 98 views
1

我正在玩MVVM Light以整合到VB.NET項目中。我已經設置了一個小例子,但它不起作用。的結合似乎只去一個方向的視圖模型,但沒有通知在視圖到達:MVVM Light和VB.NET的數據綁定

<Application x:Class="Application" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="clr-namespace:mvvmlight2" 
    StartupUri="MainWindow.xaml" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    d1p1:Ignorable="d" 
    xmlns:d1p1="http://schemas.openxmlformats.org/markup-compatibility/2006"> 

<Application.Resources> 

    <vm:ViewModelLocator x:Key="Locator" d:IsDataSource="True" xmlns:vm="clr-namespace:mvvmlight2.mvvmlight2.ViewModel" /> 

</Application.Resources> 
</Application> 

模型徑直向前

Namespace mvvmlight2.ViewModel 

Public Class MainViewModel 
    Inherits ViewModelBase 

    Public Property Size As Integer 

.... 

定位器

Namespace mvvmlight2.ViewModel 

    Public Class ViewModelLocator 

    Public Sub New() 
    ServiceLocator.SetLocatorProvider(Function() SimpleIoc.[Default]) 
    SimpleIoc.Default.Register(Of MainViewModel)() 
    End Sub 

    Public ReadOnly Property Main As MainViewModel 
    Get 
    Return ServiceLocator.Current.GetInstance(Of MainViewModel) 
    End Get 
    End Property 

    End Class 

End Namespace 

視圖

DataContext="{Binding Path=Main, Source={StaticResource Locator}}" 
... 
<Label Content="{Binding Size}" /> 
<Slider Value="{Binding Path=Size}" /> 

滑塊更改模型中的值,如我在調試器中看到的。但標籤內容不會改變。

我該怎麼辦?

TIA

+0

是否調用了OnPropertyChange的大小?順便說MS說「避免數據綁定到Label.Content屬性」https://msdn.microsoft.com/en-us/library/bb613560%28v=vs.100%29.aspx使用textblock代替 – adminSoftDK

回答

0

你的屬性必須觸發PropertyChanged事件,這在MVVMLight,通過RaisePropertyChanged()功能來完成。在C#中,有片段可用,這創造樣特性:

/// <summary> 
/// The <see cref="SummaryVisible" /> property's name. 
/// </summary> 
public const string SummaryVisiblePropertyName = "SummaryVisible"; 

private bool _summaryVisible = false; 

/// <summary> 
/// Sets and gets the ShowSummary property. 
/// Changes to that property's value raise the PropertyChanged event. 
/// </summary> 
public bool SummaryVisible 
{ 
    get 
    { 
     return _summaryVisible; 
    } 
    set 
    { 
     if (_summaryVisible == value) 
     { 
      return; 
     } 
     _summaryVisible = value; 
     RaisePropertyChanged(() => SummaryVisible); 
    } 
} 

我相信你可以直接翻譯成VB。

編輯

已經只是看着我的版本的Visual Studio,如果你安裝了MVVMLight Toolkit Extension,你得到的VB代碼段爲好。