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

早过忘川 提交于 2019-12-22 18:49:13

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/4215521/default-value-for-the-options-property-cannot-be-bound-to-a-specific-thread

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!