1.数据绑定
为ViewModel添加属性
public class AppViewModel : PropertyChangedBase
{
private int _count = 50;
public int Count
{
get { return _count; }
set
{
_count = value;
NotifyOfPropertyChange(() => Count);
}
}
}
在View中 添加一个TextBlock
注意: Name="Count" Caliburn提供的快捷数据绑定功能
For elements that display data such as TextBlock or TextBox, setting their name to match a property on the data model will automatically hook it up for you.
<Grid MinWidth="300" MinHeight="300" Background="LightBlue">
<TextBlock Name="Count" Margin="20" FontSize="150" VerticalAlignment="Center" HorizontalAlignment="Center" />
</Grid>
2.事件绑定
为ViewModel添加方法
public void IncrementCount()
{
Count++;
}
在View中 添加一个Button
注意 Name="IncrementCount" Caliburn提供的快捷事件绑定功能
<Grid MinWidth="300" MinHeight="300" Background="LightBlue">
<RepeatButton Name="IncrementCount" Content="Up" Margin="15" VerticalAlignment="Top" />
<TextBlock Name="Count" FontSize="150" VerticalAlignment="Center" HorizontalAlignment="Center" />
</Grid>
3.事件警卫
When Caliburn Micro automatically hooks up the Click event to the IncrementCount method, it also looks for a method or property called CanIncrementCount.
通过添加一个CanIncrementCount方法或属性,您可以添加额外的逻辑,基于模型的当前状态决定事件是允许处理。
决定是否可以调用该方法
public bool CanIncrementCount
{
get { return Count < 100; }
}
Since this logic is based on the value of the Count property, we also need to raise property change notification for the CanIncrementCount property whenever the Count value changes.
NotifyOfPropertyChange(() => CanIncrementCount);
Once the limit has been reached, the button will become disabled and prevent the user from further incrementing the value.
http://www.mindscapehq.com/blog/index.php/2012/1/16/caliburn-micro-part-2-data-binding-and-events/