Quantcast
Channel: Caliburn.Micro: Xaml Made Easy
Viewing all articles
Browse latest Browse all 1760

Updated Wiki: Cheat Sheet

$
0
0

Cheat Sheet

This serves as a quick guide to the most frequently used conventions and features in the Caliburn.Micro project.

Wiring Events

This is automatically wiring events on controls to call methods on the ViewModel.

Convention


<Buttonx:Name="Save">

This will cause the Click event of the Button to call "Save" method on the ViewModel.

Short Syntax


<Buttoncal:Message.Attach="Save">

This will again cause the "Click" event of the Button to call "Save" method on the ViewModel.

Different events can be used like this:

<Buttoncal:Message.Attach="[Event MouseEnter] = [Action Save]">

Different parameters can be passed to the method like this:

<Buttoncal:Message.Attach="[Event MouseEnter] = [Action Save($this)]">
  • $eventArgs– Passes the EventArgs or input parameter to your Action. Note: This will be null for guard methods since the trigger hasn’t actually occurred.
  • $dataContext– Passes the DataContext of the element that the ActionMessage is attached to. This is very useful in Master/Detail scenarios where the ActionMessage may bubble to a parent VM but needs to carry with it the child instance to be acted upon.
  • $source– The actual FrameworkElement that triggered the ActionMessage to be sent.
  • $view - The view (usually a UserControl or Window) that is bound to the ViewModel.
  • $executionContext - The action's execution context, which contains all the above information and more. This is useful in advanced scenarios.
  • $this - The actual UI element to which the action is attached. In this case, the element itself won't be passed as a parameter, but rather its default property.

Long Syntax


<UserControlx:Class="Caliburn.Micro.CheatSheet.ShellView"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"xmlns:cal="http://www.caliburnproject.org"><StackPanel><TextBoxx:Name="Name"/><ButtonContent="Save"><i:Interaction.Triggers><i:EventTriggerEventName="Click"><cal:ActionMessageMethodName="Save"><cal:ParameterValue="{Binding ElementName=Name, Path=Text}"/></cal:ActionMessage></i:EventTrigger></i:Interaction.Triggers></Button></StackPanel></UserControl>

This syntax is Expression Blend friendly.

Databinding

This is automatically binding dependency properties on controls to properties on the ViewModel.

Convention


<TextBoxx:Name="FirstName"/>

Will cause the "Text" property of the TextBox to be bound to the "FirstName" property on the ViewModel.

Explicit


<TextBoxText="{Binding Path=FirstName, Mode=TwoWay}"/>

This is the normal way of binding properties.

Composition

to do

View-Model First

to do

View-First

to do

Window Manager

to do

Event Aggregator

The three different methods on the Event Aggregator are:

publicinterface IEventAggregator {  
    void Subscribe(object instance);  
    void Unsubscribe(object instance);  
    void Publish(object message, Action<System.Action> marshal);  
}

An event can be a simple class such as:

publicclass MyEvent {
    public MyEvent(string myData) {
        this.MyData = myData;
    }

    publicstring MyData { get; privateset; }
}


Viewing all articles
Browse latest Browse all 1760

Trending Articles