Eidan
Eidan

Reputation: 33

UserControl Close Trigger don't Fire

For this problem, let me explain the environment.

I have a UserControl in XAML, I can't reach the code behind, I've to do all in the XAML part. I don't have any access to the Window code also. Only the usercontrolwho have to save the window position.

The problem is : my close trigger never fired (but this trigger work fine with a Cancel button click further in the code)

I can't reach the only part of code I've access to. I can't put the trigger before the resources, because of the userCommandBuilder.

The trigger.

<UserControl     
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
...otherstuff...
MinWidth="946" MinHeight="902" MaxWidth="946" MaxHeight="902">
  <UserControl.Resources.../>
  <i:Interaction.Triggers>
            <i:EventTrigger EventName="Close">
            <i:InvokeCommandAction Command="{x:Static addin:AddInCommands.ExecuteCmdLine}" >
                    <i:InvokeCommandAction.CommandParameter>
                        <MultiBinding Converter="{StaticResource userCommandBuilder}" ConverterParameter="SRSAVEPOSITION -T {0} -L {1}">
                        <Binding RelativeSource="{RelativeSource AncestorType={x:Type Window}}" Path="Top"  />
                        <Binding RelativeSource="{RelativeSource AncestorType={x:Type Window}}" Path="Left"  />
                    </MultiBinding>
                </i:InvokeCommandAction.CommandParameter>
            </i:InvokeCommandAction>
        </i:EventTrigger>
  </i:Interaction.Triggers>

What did I wrong ? How to solve that ?

Cancel button code

<Button Margin="0,0,0,0" IsCancel="True"  FontSize="13" FontFamily="Segoe UI">Cancel>
                        <i:Interaction.Triggers>
                            <i:EventTrigger EventName="Click">
                                <i:InvokeCommandAction Command="{x:Static addin:AddInCommands.ExecuteCmdLine}" >
                                    <i:InvokeCommandAction.CommandParameter>
                                        <MultiBinding Converter="{StaticResource userCommandBuilder}" ConverterParameter="SRSAVEPOSITION -T {0} -L {1}">
                                            <Binding RelativeSource="{RelativeSource AncestorType={x:Type Window}}" Path="Top"  />
                                            <Binding RelativeSource="{RelativeSource AncestorType={x:Type Window}}" Path="Left"  />
                                        </MultiBinding>
                                    </i:InvokeCommandAction.CommandParameter>
                                </i:InvokeCommandAction>
                                <i:InvokeCommandAction Command="ApplicationCommands.Close" />
                            </i:EventTrigger>
                        </i:Interaction.Triggers>
                    </Button>

Upvotes: 0

Views: 392

Answers (1)

mm8
mm8

Reputation: 169260

A UserControl has no Close event. If you want to handle the Closing or Closed event of the parent window in the UserControl class, you have to write some code. You cannot do this in XAML.

You could for example get a reference to the Window once the UserControl has been loaded and execute your command:

public partial class UserControl1 : UserControl
{
    public UserControl1()
    {
        InitializeComponent();
        Loaded += (s, e) => 
        {
            Window window = Window.GetWindow(this);
            if(window != null)
                window.Closing += Window_Closing;
        };
    }

    private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
    {
        AddInCommands.ExecuteCmdLine.Execute(...);
    }
}

Upvotes: 1

Related Questions