Search This Blog

Friday, September 23, 2011

Silverlight Passing event from Theme to View; Passing button's click as event from Theme to View's class property

View:

<controls:ChildWindow  [....]
Name="Window">

<perspControls:WizardControl Background="Red" TabStripPlacement="Left" TabNavigation="Once" >
                   
    <!-- Wizzard events -->
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="Cancelling">
            <ei:ChangePropertyAction PropertyName="DialogResult" Value="False" TargetName="Window" />
        </i:EventTrigger>
    </i:Interaction.Triggers>

    <!-- Wizzard items -->
    <perspControls:WizardControl.Items>
        [... some items here ...]
    </perspControls:WizardControl.Items>
   
</perspControls:WizardControl>

ViewModel:

 

Theme:

<ResourceDictionary
    [....]
    xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
    xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
    >

    <Style TargetType="controls:WizardControl">
    [.....]
   
        <Setter Property="Template">
       
        [....]
       
            <Grid x:Name="TemplateLeft" >
            [...]
           
                <!-- Wizzard buttons -->
                <Grid Grid.Row="1">
                    <StackPanel HorizontalAlignment="Right" Orientation="Horizontal">
                        <Button HorizontalAlignment="Right" Content="Wstecz" Command="{Binding WsteczCommand}" Width="70" />
                        <Button HorizontalAlignment="Right" Content="Dalej" Command="{Binding DalejCommand}" Width="70" />
                        <Button x:Name="CancelButton" Content="Anuluj" Command="{Binding CancelCommand}" DataContext="{TemplateBinding DataContext}"  />
                    </StackPanel>
                </Grid>
               
            </Grid>
           
           
Control Class:

public class WizardControl : TabControl
{
    public WizardControl()
    {
        DefaultStyleKey = typeof(WizardControl);
    }

    public override void OnApplyTemplate()
    {
        base.OnApplyTemplate();
        var cancelButton = (GetTemplateChild("CancelButton") as Button);
        cancelButton.Click += (s, e) =>
        {
            if (Cancelling != null)
            {
                Cancelling(this, e);
            }
        };
    }

    public event RoutedEventHandler Cancelling;
}       

No comments:

Post a Comment

If you like this post, please leave a comment :)