none
关于自定义控件时出现System.Reflection.TargetInvocationException问题 RRS feed

  • 问题

  • ResourceDictionary里的XMAL是这样的:

      <Style TargetType="dmpToolkit:HeaderNavigation">
    
        <Setter Property="Template">
          <Setter.Value>
            <ControlTemplate TargetType="dmpToolkit:HeaderNavigation">
              <Grid x:Name="RootElement">
                <Grid.ColumnDefinitions>
                  <ColumnDefinition Width="Auto" />
                  <ColumnDefinition Width="Auto" />
                  <ColumnDefinition Width="Auto" />
                  <ColumnDefinition Width="Auto" />
                  <ColumnDefinition Width="Auto" />
                </Grid.ColumnDefinitions>
                
                <Button Grid.Column="0">
                  <Button.Template>
                    <ControlTemplate TargetType="Button">
                      <Border x:Name="PreButtonBorderElement"
                          BorderBrush="{TemplateBinding PreButtonBorderBrush}"
                          Width="15"
                          Height="15"
                          BorderThickness="1"
                          Background="Black"
                          CornerRadius="2">
                        <Path StrokeThickness="1.5"
                           Data="m 6,3 L 2,6 L 6,9 m 4,-6 L 6,6 L 10,9"
                           Stroke="White">
                        </Path>
                      </Border>
                    </ControlTemplate>
                  </Button.Template>
                </Button>
    
                <TextBlock x:Name="PreHeaderTextElement"
                      Grid.Column="1"
                      Margin="5,0,5,0"
                      HorizontalAlignment="Center"
                      VerticalAlignment="Center"
                      FontWeight="Bold"
                      FontSize="13"
                      Text="{TemplateBinding PreHeader}"></TextBlock>
    
    ......

    然后cs文件是这样的,我在后面定义属性PreButtonBorderBrush:

      [TemplatePart(Name = "RootElement", Type = typeof(Grid))]
      [TemplatePart(Name = "PreButtonBorderElement", Type = typeof(Border))]
    
      public class HeaderNavigation : ContentControl
      {
    public HeaderNavigation()
        {
          DefaultStyleKey = typeof(HeaderNavigation);
        }
    
           public override void OnApplyTemplate()
        {
          base.OnApplyTemplate();
        }
    
        public Brush PreButtonBorderBrush
        {
          get { return (Brush)GetValue(PreButtonBorderBrushProperty); }
          set { SetValue(PreButtonBorderBrushProperty, value); }
        }
    
            public static readonly DependencyProperty PreButtonBorderBrushProperty = DependencyProperty.Register(
          "PreButtonBorderBrush",
          typeof(Brush),
          typeof(HeaderNavigation),
          new PropertyMetadata(new SolidColorBrush(Colors.White), new PropertyChangedCallback(OnPreButtonBorderBrushPropertyChanged)));
        
        private static void OnPreButtonBorderBrushPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
          (d as HeaderNavigation).OnPreButtonBorderBrushPropertyChanged(e);
        }
    
    
        private void OnPreButtonBorderBrushPropertyChanged(DependencyPropertyChangedEventArgs e)
        {
          try
          {
            Border border = this.GetTemplateChild("PreButtonBorderElement") as Border;
            border.BorderBrush = e.NewValue as Brush;
          }
          catch
          {
          }
        }
    ......

    在编译时就出现System.Reflection.TargetInvocationException
    Exception has been thrown by the target of an invocation.

    System.Exception
    Error HRESULT E_FAIL has been returned from a call to a COM component.

    我错误就是在设置PreButtonBorderBrush时出现的,这是怎么回事??

    怎么解决??谢谢

    2010年6月25日 4:10