简体   繁体   English

WPF-添加ObjectDataProvider破坏了我的设计师

[英]WPF - Adding a ObjectDataProvider breaks my designer

Perhaps i'm not using the ObjectDataProvider correctly, but im following the MSDN examples so im not sure whats going wrong. 也许我没有正确使用ObjectDataProvider,但是我遵循MSDN示例,所以我不确定出什么问题。

Goal: When i click a button, it will close the window by calling a method "exitButtonMethod" which simple does this.Close();. 目标:当我单击一个按钮时,它将通过调用方法“ exitButtonMethod”关闭窗口,该方法简单地执行this.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>

The error is that it simply breaks my designer and gives me the following error in the designer: 错误是,这只会打断我的设计师,并给我以下设计师错误:

System.Runtime.Remoting.RemotingException
[228940] Designer process terminated unexpectedly!

What the purpose of ObjectDataProvider is to create objects in XAML that you can bind to. ObjectDataProvider的目的是在XAML中创建可以绑定到的对象。 You can also use it to bind to the results of calling a method on that object, which is what you are trying to do. 您也可以使用它绑定到在该对象上调用方法的结果,这就是您要尝试执行的操作。

Here you are making an new object with the type of Window1 and binding to the method callExitButtonMethod . 在这里,您将创建一个类型为Window1并绑定到方法callExitButtonMethod对象。 So you are unintentionally making a new window inside your window. 因此,您无意中在窗口内创建了一个新窗口。

<ObjectDataProvider ObjectType="{x:Type local:Window1}"
                  MethodName="exitButtonMethod" x:Key="callExitButtonMethod">
    </ObjectDataProvider>

Now that new window, when created, also has a window inside of it... etc. and you get an infinite loop of making windows. 现在,新窗口在创建时也将在其中包含一个窗口...等等,您将获得制作窗口的无限循环。

This is why you are getting a stack overflow. 这就是为什么堆栈会溢出的原因。

What you are trying to do is much simpler than what you are currently doing. 您正在尝试做的事情比当前正在做的事情要简单得多。 In order to call a method on a button when you click it, you can simply do: 为了在单击按钮时调用按钮上的方法,可以简单地执行以下操作:

<Button Click="NameOfMethodHere" />

In your case, just add the Click parameter to your button and get rid of the ObjectDataProvider . 在您的情况下,只需将Click参数添加到您的按钮中,然后删除ObjectDataProvider

EDIT: Also to set events from a Style, see EventSetter . 编辑:也可以通过样式设置事件,请参见EventSetter

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM