通过对这个问题的慷慨帮助,我整理了以下MVVM结构,它在XAML(当前日期/时间)中实时显示模型的变化,非常好.
这方面的一个凉爽的优势建立的是,当你看到你的观点在设计模式中的Visual Studio或混合的,你看时间流逝的,这意味着在设计时可以访问从模型的实时数据.
在让它工作的过程中,我惊讶地发现大部分批量从我的ViewModel移动到我的模型中,包括INotifyPropertyChange的实现.另一个变化是我不再绑定ViewModel上的属性而是绑定到方法.
所以目前这是我最喜欢的MVVM风格:
视野愚蠢:
ViewModel是瘦的:
型号很胖:
问题:
如果您只是将XAML和代码复制到新的WPF项目中,则以下代码将起作用.
XAML:
<Window x:Class="TestBinding99382.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TestBinding99382"
Title="Window1" Height="300" Width="300">
<Window.Resources>
<ObjectDataProvider
x:Key="DataSourceCustomer"
ObjectType="{x:Type local:ShowCustomerViewModel}"
MethodName="GetCurrentCustomer"/>
</Window.Resources>
<DockPanel DataContext="{StaticResource DataSourceCustomer}">
<StackPanel DockPanel.Dock="Top" Orientation="Horizontal">
<TextBlock Text="{Binding Path=FirstName}"/>
<TextBlock Text=" "/>
<TextBlock Text="{Binding Path=LastName}"/>
</StackPanel>
<StackPanel DockPanel.Dock="Top" Orientation="Horizontal">
<TextBlock Text="{Binding Path=TimeOfMostRecentActivity}"/>
</StackPanel>
</DockPanel>
</Window>
Run Code Online (Sandbox Code Playgroud)
代码背后:
using …Run Code Online (Sandbox Code Playgroud)