我正在玩MVVM Light以集成到VB.NET项目中。我设置了一个最小的例子,但它不起作用。绑定似乎只向viewmodel的一个方向发送,但没有通知到达视图:
<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 :(得分: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片段。