0

Perhaps i'm not using the ObjectDataProvider correctly, but im following the MSDN examples so im not sure whats going wrong.

Goal: When i click a button, it will close the window by calling a method "exitButtonMethod" which simple does 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!
shawn a
  • 799
  • 3
  • 13
  • 21
  • But does your code actually run correctly? I personally have had many terrible experiences with the Visual Studio designer just being bad. It is annoying but if your code works I wouldn't worry too much. – Omada Jan 11 '15 at 19:12
  • I get a stackoverflow exception at runtime :( – shawn a Jan 11 '15 at 19:28

1 Answers1

1

What the purpose of ObjectDataProvider is to create objects in XAML that you can bind to. 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. 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.

EDIT: Also to set events from a Style, see EventSetter.

Omada
  • 784
  • 4
  • 11