Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Default value for the 'Options' property cannot be bound to a specific thread

I find that when I change a class from

public class MarkdownEditorOptions : ObservableObject

to

public class MarkdownEditorOptions : INotifyPropertyChanged, DependencyObject

as I wanted to use dependency properties, I get the error

Default value for the 'Options' property cannot be bound to a specific thread. ...\Views\ShellView.xaml

Options is declared as a dependency property on ShellViewModel

public MarkdownEditorOptions Options
{
    get { return (MarkdownEditorOptions)GetValue(OptionsProperty); }
    set { SetValue(OptionsProperty, value); }
}

public static readonly DependencyProperty OptionsProperty =
    DependencyProperty.Register("Options", typeof(MarkdownEditorOptions), typeof(ShellViewModel), new UIPropertyMetadata(new MarkdownEditorOptions()));

whats wrong?

like image 552
Jiew Meng Avatar asked Oct 19 '25 14:10

Jiew Meng


1 Answers

See these questions

  • Why Would a Dependency-Property Implementation Crash My Application When I Provide a Default Value?
  • Attached Property: 'System.TypeInitializationException' when setting default value

Your Dependency property is not thread safe, meaning that it doesn't inherit from System.Windows.Freezable.
Change DependencyObject to Freezable and it'll work since Freezable derives from DependencyObject.

like image 98
Fredrik Hedblad Avatar answered Oct 22 '25 06:10

Fredrik Hedblad