【问题标题】:UserControl Close Trigger don't Fire用户控件关闭触发器不触发
【发布时间】:2017-10-23 11:54:56
【问题描述】:

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

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

问题是:我的关闭触发器从未触发(但此触发器可以在代码中进一步单击“取消”按钮时正常工作)

我无法访问我可以访问的唯一部分代码。 由于 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>

【问题讨论】:

  • 你有Cancel的代码吗?所以我们可以比较。
  • 是的,我明天会带着取消按钮代码回来。
  • 已添加代码。与关闭事件没有什么不同。但是如果用户控件上不存在关闭事件或卸载事件,我不知道如何做到这一点:)

标签: c# wpf xaml triggers user-controls


【解决方案1】:

UserControl 没有 Close 事件。如果要在UserControl类中处理父窗口的ClosingClosed事件,就得写一些代码。您不能在 XAML 中执行此操作。

例如,一旦加载了UserControl,您就可以获取对Window 的引用并执行您的命令:

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

【讨论】:

  • 我不能使用后面的代码,这是工作的困难部分^^ 但我认为我们已经找到了解决方案。很快就会回来回答。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-11
  • 1970-01-01
  • 1970-01-01
  • 2016-06-18
相关资源
最近更新 更多