none
wpf user control problem. Default value type does not match type of property.

    Question

  • I have created a simple WPF user control. Then I add that control to a basic WPF window application. The solution I built consists of a WPF custom control library project. Then a 2nd WPF application project.  Everything builds ok. But when I run the WPF application I get an error: "XamlParseException The type initializer threw an exception. Default value type does not match type of property 'ElementId'."

    The error has to do with the one DependencyProperty in the user control named ElementId.  Its type is "string".  Where is the default value type being assigned from and how do I get WPF to assign a value of the correct type?

    Here is my code. First the code behind xaml.cs of the user control.  Then the Window.xaml of the WPF application. Last is the xaml of the user control which I am guessing does not matter much.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    
    namespace WpfControls
    {
      /// <summary>
      /// Interaction logic for Steve3.xaml
      /// </summary>
      public partial class Steve3 : UserControl
      {
        public Steve3()
        {
          InitializeComponent();
        }
    
        public static readonly DependencyProperty ElementIdProperty =
          DependencyProperty.Register(
          "ElementId", typeof(string), typeof(Steve3),
          new PropertyMetadata(0));
    
        public string ElementId
        {
          get { return (string)GetValue(ElementIdProperty); }
          set { SetValue(ElementIdProperty, value); }
        }
    
        private void RadioButton_Click(object sender, RoutedEventArgs e)
        {
          RadioButton rb = (RadioButton)e.Source;
          ElementId = (string)rb.Content;
        }
    
      }
    }
    
    <Window x:Class="Tester.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            
            xmlns:lib="clr-namespace:WpfControls;assembly=WpfControls"
            
        Title="Window1" Height="300" Width="300">
        <Grid>
            <lib:Steve3></lib:Steve3>
            
        </Grid>
    </Window>
    
    <UserControl x:Class="WpfControls.Steve3"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Height="300" Width="300">
    
        <Grid Name="grid">
            <GroupBox Header="Xaml element" HorizontalAlignment="Left" VerticalAlignment="Top">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition />
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition></ColumnDefinition>
                        <ColumnDefinition></ColumnDefinition>
                    </Grid.ColumnDefinitions>
                    <StackPanel Orientation="Vertical">
                        <RadioButton Content="RowDefinition" GroupName="xe" Margin="2" Click="RadioButton_Click"></RadioButton>
                        <RadioButton Content="Grid.RowDefinitions" GroupName="xe" Margin="2" Click="RadioButton_Click"></RadioButton>
                        <RadioButton Content="GroupBox" GroupName="xe" Margin="2" Click="RadioButton_Click"></RadioButton>
                        <RadioButton Content="StackPanel" GroupName="xe" Margin="2" Click="RadioButton_Click"></RadioButton>
                    </StackPanel>
                    <StackPanel Orientation="Vertical" Grid.Column="1">
                        <RadioButton Content="RadioButton" GroupName="xe" Margin="2" Click="RadioButton_Click"></RadioButton>
                        <RadioButton Content="Image" GroupName="xe" Margin="2" Click="RadioButton_Click"></RadioButton>
                    </StackPanel>
                </Grid>
            </GroupBox>
        </Grid>
    
    
    
    </UserControl>
    



    Monday, November 30, 2009 12:52 AM

Answers

  • Hello,

    Change this:

     public static readonly DependencyProperty ElementIdProperty =
          DependencyProperty.Register("ElementId", typeof(string), typeof(Steve3),new PropertyMetadata(string.Empty ));

    Good Luck!
    • Marked as answer by Steve Richter Monday, November 30, 2009 10:01 AM
    Monday, November 30, 2009 1:35 AM

All replies

  • Hello,

    Change this:

     public static readonly DependencyProperty ElementIdProperty =
          DependencyProperty.Register("ElementId", typeof(string), typeof(Steve3),new PropertyMetadata(string.Empty ));

    Good Luck!
    • Marked as answer by Steve Richter Monday, November 30, 2009 10:01 AM
    Monday, November 30, 2009 1:35 AM
  • thanks.  

    WPF sure is complicated.  Could this Register method be coded as a generic, eliminating the dependency between those two arguments?
    Monday, November 30, 2009 10:05 AM
  • here is my generic version of DependencyProperty.Register:

      public static class DependencyPropertyStatics
      {
        public static DependencyProperty Register<T>( string InPropName, Type InOwningClass )
        {
         
          DependencyProperty prop = DependencyProperty.Register(
            InPropName, typeof(T), InOwningClass,
            new PropertyMetadata(default(T)));
          return prop;
        }
      }


        public static readonly DependencyProperty ElementIdProperty =
          DependencyPropertyStatics.Register<string>(
          "ElementId", typeof(Steve3)) ;
    Monday, November 30, 2009 10:26 AM