C# 用户控制关闭触发器不触发

C# 用户控制关闭触发器不触发,c#,wpf,xaml,triggers,user-controls,C#,Wpf,Xaml,Triggers,User Controls,对于这个问题,让我解释一下环境 我在XAML中有一个UserControl,我无法访问后面的代码,我必须在XAML部分完成所有工作。我也没有任何访问窗口代码的权限。只有必须保存窗口位置的用户控件 问题是:我的关闭触发器从未触发,但如果在代码中单击“取消”按钮,则此触发器工作正常 我无法访问代码中唯一可以访问的部分。 我不能将触发器放在资源之前,因为userCommandBuilder 扳机 <UserControl xmlns:i="clr-namespace:System.Wi

对于这个问题,让我解释一下环境

我在XAML中有一个UserControl,我无法访问后面的代码,我必须在XAML部分完成所有工作。我也没有任何访问窗口代码的权限。只有必须保存窗口位置的用户控件

问题是:我的关闭触发器从未触发,但如果在代码中单击“取消”按钮,则此触发器工作正常

我无法访问代码中唯一可以访问的部分。 我不能将触发器放在资源之前,因为userCommandBuilder

扳机

<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>
我做错了什么? 如何解决这个问题

取消按钮代码

<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>
UserControl没有关闭事件。如果要处理UserControl类中父窗口的关闭或关闭事件,必须编写一些代码。在XAML中无法执行此操作

例如,加载UserControl并执行命令后,可以获取对窗口的引用:

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(...);
    }
}

你有取消的密码吗?所以我们可以比较。是的,我明天回来时带着取消按钮代码。添加了代码。与关闭事件没有什么不同。但是,如果usercontrol上不存在close事件或unloaded事件,我不知道如何这样做:我不能使用隐藏的代码,这是工作的困难部分^^^^,但我认为我们已经找到了解决方案。我很快就会回来给你答案。