'System.Windows.Controls.Button'的初始化引发了异常。 - WPF

时间:2012-07-19 06:50:26

标签: wpf xaml controltemplate

我收到了上述异常。我试图调试,但无法找到合适的解决方案。我在下面发布我的XAML代码。如何解决这个异常?我正在尝试学习控制模板,并试图从书中学习一个例子。但是,只要我运行我的代码,就会弹出这个异常。

<Window x:Class="ControlTemplateMCTS.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Control Template MCTS Example" Height="350" Width="525">

   <Window.Resources>

      <ControlTemplate x:Key="ButtonTemplate" TargetType="Button">

         <Grid>

            <Ellipse Name="controlTemplateEllipse" Stroke="{TemplateBinding BorderBrush}" StrokeThickness="{TemplateBinding BorderThickness}">
               <Ellipse.Fill>
                  <RadialGradientBrush GradientOrigin="0.5,0.5">
                     <GradientStop Color="Red" Offset="0"/>
                     <GradientStop Color="Orange" Offset="0.25"/>
                     <GradientStop Color="Blue" Offset="0.5"/>
                     <GradientStop Color="Green" Offset="0.75"/>
                     <GradientStop Color="Yellow" Offset="1"/>
                  </RadialGradientBrush>
               </Ellipse.Fill>
            </Ellipse>


            <!--   <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>-->

         </Grid>

         <ControlTemplate.Triggers>

            <Trigger Property="IsMouseOver" Value="True">
               <Setter TargetName="controlTemplateEllipse" Property="Fill">
                  <Setter.Value>
                     <RadialGradientBrush GradientOrigin="0.5,0.5">
                        <GradientStop Color="LightCoral" Offset="0"/>
                        <GradientStop Color="LightCyan" Offset="0.25"/>
                        <GradientStop Color="LightGray" Offset="0.5"/>
                        <GradientStop Color="LightGoldenrodYellow" Offset="0.75"/>
                        <GradientStop Color="LightPink" Offset="1"/>
                     </RadialGradientBrush>
                  </Setter.Value>
               </Setter>
            </Trigger>

            <Trigger>
               <Setter TargetName="controlTemplateEllipse" Property="Fill">
                  <Setter.Value>
                     <RadialGradientBrush GradientOrigin="0.5,0.5">
                        <GradientStop Color="Gray" Offset="0"/>
                        <GradientStop Color="Silver" Offset="0.25"/>
                        <GradientStop Color="White" Offset="0.5"/>
                        <GradientStop Color="WhiteSmoke" Offset="0.75"/>
                        <GradientStop Color="DarkGray" Offset="1"/>
                     </RadialGradientBrush>
                  </Setter.Value>
               </Setter>
            </Trigger>

            <EventTrigger RoutedEvent="Button.Click">
               <BeginStoryboard>
                  <Storyboard AutoReverse="True">
                     <DoubleAnimation To="0" Duration="0:0:5" Storyboard.TargetProperty="Width"/>
                     <DoubleAnimation To="0" Duration="0:0:5" Storyboard.TargetProperty="Height"/>
                  </Storyboard>
               </BeginStoryboard>
            </EventTrigger>

         </ControlTemplate.Triggers>

      </ControlTemplate>   

      <Style TargetType="{x:Type Button}">
         <Setter Property="Template" Value="{StaticResource ButtonTemplate}"/>
      </Style>

   </Window.Resources>

   <Grid>
      <!--<Button  Name="TestButton" Width="120" Height="28">Button One</Button>-->
      <Button  Name="DemoButton" Height="50" IsEnabled="False"  Width="120">Button Two</Button>
   </Grid>

</Window>

1 个答案:

答案 0 :(得分:2)

问题在于:

            <Trigger>
                <Setter TargetName="controlTemplateEllipse" Property="Fill">
                    <Setter.Value>
                        <RadialGradientBrush GradientOrigin="0.5,0.5">
                            <GradientStop Color="Gray" Offset="0"/>
                            <GradientStop Color="Silver" Offset="0.25"/>
                            <GradientStop Color="White" Offset="0.5"/>
                            <GradientStop Color="WhiteSmoke" Offset="0.75"/>
                            <GradientStop Color="DarkGray" Offset="1"/>
                        </RadialGradientBrush>
                    </Setter.Value>
                </Setter>
            </Trigger>

您必须初始化触发器。例如:

<Trigger Property="IsEnabled" Value="True">

这有助于WPF知道触发器应该何时工作。