WPF Design-Time Type-Cast错误

时间:2014-12-21 20:09:47

标签: c# wpf

我有一个带有Singleton模式的Settings类:

public class Settings
{
    public static Settings Instance 
    {
         get { ... /* Return instance created by custom xml deserializer*/ }
    } 
    public Settings() 
    {
        /* Constructor used by custom xml deserializer */
    }

    public bool EnableImages { get; set; }
    public bool CheckForUpdates { get; set; }
    public ObservableCollection<FavShowData> TvShows {get; set;}
    //....
}

在我的设置表单中,我绑定到这些设置:

<CheckBox IsChecked="{Binding CheckForUpdates, Source={x:Static local:Settings.Instance}}"  Content="Autocheck at start and every 30 minutes" />
<CheckBox IsChecked="{Binding EnableImages, Source={x:Static local:Settings.Instance}}"  Content="Enable Images" />

对于这两行,我收到以下设计时错误:

  

Das Objekt mit dem Typ   “System.Collections.ObjectModel.ObservableCollection 1[SjUpdater.Model.FavShowData]" kann nicht in den Typ "System.Collections.ObjectModel.ObservableCollection 1 [SjUpdater.Model.FavShowData]”   konvertiert werden。

翻译:

  

类型的对象   “System.Collections.ObjectModel.ObservableCollection 1[SjUpdater.Model.FavShowData]" can not be converted into type "System.Collections.ObjectModel.ObservableCollection 1 [SjUpdater.Model.FavShowData]”

这很奇怪,因为我从不绑定到 TvShows 属性。 编译很好,代码可以正常工作。

您对如何修复设计时错误有任何建议吗?

2 个答案:

答案 0 :(得分:0)

您是否尝试过将属性设置为静态...您正在使用一个静态类,它不允许实例构造函数,但您的属性也不会被标识为静态

public static bool EnableImages { get; set; }
public static bool CheckForUpdates { get; set; }
public static ObservableCollection<FavShowData> TvShows {get; set;}

答案 1 :(得分:0)

解决方法:将Stuff移动到ViewModel:

public class MainWindowViewModel
{
    public Settings Settings
    {
        get { return Settings.Instance; }
    }
    //....
}

将XAML更改为:

<Grid DataContext="{Binding Settings}">
    <CheckBox IsChecked="{Binding CheckForUpdates}"  Content="Autocheck at start and every 30 minutes" />
    <CheckBox IsChecked="{Binding EnableImages}"  Content="Enable Images" />
    ....
</Grid>

不再有设计时错误。