C# WPF-添加ObjectDataProvider会破坏我的设计器

C# WPF-添加ObjectDataProvider会破坏我的设计器,c#,.net,wpf,triggers,objectdataprovider,C#,.net,Wpf,Triggers,Objectdataprovider,也许我没有正确使用ObjectDataProvider,但我遵循MSDN示例,所以我不确定出了什么问题 目标:当我单击一个按钮时,它将通过调用exitButtonMethod方法来关闭窗口,simple就是这么做的。close <Window x:Class="WpfApplication1.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://s

也许我没有正确使用ObjectDataProvider,但我遵循MSDN示例,所以我不确定出了什么问题

目标:当我单击一个按钮时,它将通过调用exitButtonMethod方法来关闭窗口,simple就是这么做的。close

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:sys="clr-namespace:System;assembly=mscorlib"
    xmlns:local="clr-namespace:WpfApplication1"
    Title="Window1" WindowStyle="None" AllowsTransparency="True"
    WindowStartupLocation="CenterScreen" mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" Height="254" Width="438" Opacity="1" Background="{x:Null}">

<Window.Resources>
    <ObjectDataProvider ObjectType="{x:Type local:Window1}"
                  MethodName="exitButtonMethod" x:Key="callExitButtonMethod">
    </ObjectDataProvider>
    <Style x:Key="ExitButtons" TargetType="{x:Type Button}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate x:Name="exitButtonTemplate" TargetType="Button">
                    <Grid>
                        <Ellipse x:Name="exitButtonEllipse" Fill="#597E0000"/>
                        <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsPressed" Value="True">
                            <Setter TargetName="exitButtonEllipse" Property="Fill" Value="#897E0000" />
                            <Binding Source="{StaticResource callExitButtonMethod}"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>
<Grid Width="400" Height="200" Opacity="1">
    <Rectangle Height="200" HorizontalAlignment="Left" VerticalAlignment="Top" Name="rectangle1" Stroke="#FF7E0000" Width="400" RadiusX="40" RadiusY="40" Fill="#64860000" StrokeThickness="3" />
    <Button Style="{StaticResource ExitButtons}" Content="X" Height="25" Width="25" Margin="359,16,16,0" VerticalAlignment="Top" Focusable="True" FontSize="15" FontFamily="PMingLiU" Foreground="#FF7E0000" Opacity="1"/>
</Grid>

ObjectDataProvider的目的是在XAML中创建可以绑定的对象。您还可以使用它绑定到对该对象调用方法的结果,这正是您要做的

在这里,您将创建一个Window1类型的新对象,并绑定到方法callExitButtonMethod。因此,您无意中在窗口内创建了一个新窗口

<ObjectDataProvider ObjectType="{x:Type local:Window1}"
                  MethodName="exitButtonMethod" x:Key="callExitButtonMethod">
    </ObjectDataProvider>
现在,新窗口在创建时,内部也有一个窗口。。。等等,你会得到一个无限循环的窗口

这就是为什么会出现堆栈溢出

你正在尝试做的事情比你现在正在做的要简单得多。要在单击按钮时调用该按钮上的方法,只需执行以下操作:

在您的情况下,只需将Click参数添加到按钮中,并去掉ObjectDataProvider


编辑:要设置样式中的事件,请参见。

,但您的代码是否确实正确运行?我个人曾经有过很多糟糕的经历,比如VisualStudio设计师的糟糕表现。这很烦人,但如果您的代码正常工作,我不会太担心。我在运行时遇到stackoverflow异常:
<ObjectDataProvider ObjectType="{x:Type local:Window1}"
                  MethodName="exitButtonMethod" x:Key="callExitButtonMethod">
    </ObjectDataProvider>