WPF custom control reported an error: the default value type does not match the type of the "ischecked" property.

cat endless 21 Reputation points
2021-09-26T13:37:35.01+00:00

To write a WPF custom control, add a checkbox in it, and then register a dependency to expose it to the outside world so as to obtain the ischecked status. Now an error is reported:
Error xdg0062 default value type does not match the type of 'ischecked' property

 public Nullable<Boolean> CheckBoxIsChecked
         {
             get { return (Nullable<Boolean>)GetValue(CheckBoxIsCheckedProperty); }
             set { SetValue(CheckBoxIsCheckedProperty, value); }
         }
         // Using a DependencyProperty as the backing store for ChcckBoxIsChecked.  This enables animation, styling, binding, etc...
         public static readonly DependencyProperty CheckBoxIsCheckedProperty =
             DependencyProperty.Register("IsChecked", typeof(Nullable<Boolean>), typeof(FileControl), new PropertyMetadata("CheckBox",new PropertyChangedCallback(onCheckBoxChanged)));
         static void onCheckBoxChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
         {
             ((FileControl)sender).OnCheckBoxValueChanged(args);
         }
         private void OnCheckBoxValueChanged(DependencyPropertyChangedEventArgs e)
         {
             bool temp = bool.Parse(e.NewValue.ToString());
             this.checkBox.IsChecked = (Nullable < Boolean >) temp;
         }
Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,676 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,279 questions
XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
766 questions
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 112.5K Reputation points
    2021-09-26T14:15:02.65+00:00

    Try this modification:

    static readonly DependencyProperty CheckBoxIsCheckedProperty =
       DependencyProperty.Register( "CheckBoxIsChecked", typeof( bool? ), typeof( FileControl ), new PropertyMetadata( null, onCheckBoxChanged  ) );
    
    0 comments No comments

0 additional answers

Sort by: Most helpful