C# 如何为模板元素执行事件?

C# 如何为模板元素执行事件?,c#,.net,wpf,xaml,user-controls,C#,.net,Wpf,Xaml,User Controls,我在WPF中是否有机会在模板中为控件执行事件而不创建UserControl等等? 例如:创建的窗口模板具有自定义的“关闭(X)”按钮。它对每个窗口都有相同的操作。有机会让它工作吗?单击将关闭窗口的事件 我的意思是这样使用它: <Window style="{StaticResource MyWindowTemplate}">...</Window> 。。。 并且不创建自定义窗口类,因为我希望有机会将其用于每个窗口类 那么有机会这样做吗?或者任何类似或更好的解决方案?我

我在WPF中是否有机会在模板中为控件执行事件而不创建UserControl等等? 例如:创建的窗口模板具有自定义的“关闭(X)”按钮。它对每个窗口都有相同的操作。有机会让它工作吗?单击将关闭窗口的事件

我的意思是这样使用它:

<Window style="{StaticResource MyWindowTemplate}">...</Window>
。。。
并且不创建自定义窗口类,因为我希望有机会将其用于每个窗口类


那么有机会这样做吗?或者任何类似或更好的解决方案?

我认为
模板
无法实现行为。它们代表的是外观和感觉,而不是行为。对于行为,我们附加了属性和行为,这些属性和行为在附加到其有效的目标依赖项对象时表现相同

e、 g.在您的情况下,右上角的关闭按钮是一个困难的按钮,但窗口上的任何按钮都会关闭目标UI,即窗口本身,如果指定了某些附加行为

 <Window x:Class="..."
         ...>
    <Grid>
        <Grid.RowDefinitions>
           <RowDefinition Height="*"/>
           <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <ContentControl ... />
        <Buton Width="100"
               Content="Close"
               local:CloseBehavior.IsCloseButton="True" />
   </Grid>
 </Window>

我希望这有帮助。

好的。我如何对这种行为进行编码?您还可以为这种行为添加代码吗?我希望它不应该被编码在每个窗口…请看我的代码上面。您不必为每个窗口或按钮分别编写此行为。使用此方法,您还可以更新OnIsCloseButtonPropertyChanged()方法,以便根据需要在“关闭”按钮上应用工具提示、样式、模板和更多行为。可能是我太快了。我有一个错误“在类型“CloseBehavior”中找不到可附加属性“IsCloseButton”,它在我的案例中非常有效。你的本地名称空间指的是什么?不是
CloseBehavior
是一个静态类。它不必在您的资源中实例化。附加属性的工作方式是通过静态声明。因此,我询问
local
名称空间是否正确定义了类(按照我上面解释的方式),然后通过名称空间引用它应该可以工作。
public static class CloseBehavior
{
    public static readonly DependencyProperty IsCloseButtonProperty
        = DependencyProperty.RegisterAttached(
            "IsCloseButton",
            typeof (bool),
            typeof (CloseBehavior),
            new PropertyMetadata(
               false,
               OnIsCloseButtonPropertyChanged));

    private static void OnIsCloseButtonPropertyChanged
        (DependencyObject depObj,
         DependencyPropertyChangedEventArgs e)
    {
        var buttonBase = depObj as ButtonBase;
        if (buttonBase != null && (bool)e.NewValue)
        {
            buttonBase.Click
                += (o, args) =>
                    {
                        var window
                            = GetVisualLogicalParent(
                                  buttonBase,
                                  typeof(Window)) as Window;

                        if (window != null)
                        {
                            window.Close();
                        }
                    };
        }
    }

    public static bool GetIsCloseButton(DependencyObject depObj)
    {
        return (bool)depObj.GetValue(IsCloseButtonProperty);
    }

    public static void SetIsCloseButton(
        DependencyObject depObj,
        bool value)
    {
        depObj.SetValue(IsCloseButtonProperty, value);
    }

    public static DependencyObject GetVisualLogicalParent(
       DependencyObject depObj,
       Type type)
    {
        if (depObj != null)
        {
            var parent = VisualTreeHelper.GetParent(depObj);
            if (parent == null)
            {
                parent = LogicalTreeHelper.GetParent(depObj);
            }
            if (parent != null)
            {
                if (type.IsInstanceOfType(parent))
                {
                    return parent;
                }
                else
                {
                    return GetVisualLogicalParent(parent, type);
                }
            }
        }

        return null;
    }
}