问题
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