【问题标题】:WPF - Adding a ObjectDataProvider breaks my designerWPF - 添加 ObjectDataProvider 会破坏我的设计器
【发布时间】:2015-01-11 18:52:07
【问题描述】:

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

目标:当我点击一个按钮时,它将通过调用一个简单的方法“exitButtonMethod”来关闭窗口。关闭();。

<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>

错误在于它只是破坏了我的设计器并在设计器中给了我以下错误:

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

【问题讨论】:

  • 但是你的代码真的运行正确吗?我个人有过很多糟糕的 Visual Studio 设计师的糟糕经历。这很烦人,但如果你的代码有效,我不会太担心。
  • 我在运行时遇到 stackoverflow 异常 :(

标签: c# .net wpf triggers objectdataprovider


【解决方案1】:

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

在这里,您正在创建一个类型为Window1 并绑定到方法callExitButtonMethod 的new 对象。所以你无意中在你的窗口中创建了一个新窗口。

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

现在,新窗口在创建时也有一个窗口...等等,您将获得创建窗口的无限循环。

这就是堆栈溢出的原因。

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

&lt;Button Click="NameOfMethodHere" /&gt;

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

编辑:还要从样式中设置事件,请参阅EventSetter。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-10-08
    • 2019-12-10
    • 1970-01-01
    • 2014-09-10
    • 1970-01-01
    • 1970-01-01
    • 2018-11-28
    • 1970-01-01
    相关资源
    最近更新 更多