[英]WPF Design-Time Type-Cast error
I have a Settings class, with a Singleton pattern: 我有一个具有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;}
//....
}
In my Settings Form I bind to those settings: 在我的设置表单中,我绑定到这些设置:
<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" />
For both lines I'm getting the following design-time error: 对于这两行,我都收到以下设计时错误:
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.ObservableCollection1[SjUpdater.Model.FavShowData]" kann nicht in den Typ "System.Collections.ObjectModel.ObservableCollection
1 [SjUpdater.Model.FavShowData]”的示例。
Translated: 已翻译:
Object of type "System.Collections.ObjectModel.ObservableCollection
1[SjUpdater.Model.FavShowData]" can not be converted into type "System.Collections.ObjectModel.ObservableCollection
1[SjUpdater.Model.FavShowData]"1[SjUpdater.Model.FavShowData]" can not be converted into type "System.Collections.ObjectModel.ObservableCollection
1 [SjUpdater.Model.FavShowData]”的对象1[SjUpdater.Model.FavShowData]" can not be converted into type "System.Collections.ObjectModel.ObservableCollection
1 [SjUpdater.Model.FavShowData]”的对象
This is very strange, since I never bind to the TvShows Property. 这很奇怪,因为我从未绑定到TvShows属性。 Compilation is fine and the code works as desired. 编译很好,代码可以按需工作。
Do you have any suggestions on how to fix that design-time error? 您对如何解决设计时错误有任何建议?
Have you tried making the properties as static too... You are working a static class which doesn't allow an instance constructor, yet your properties are not identified as static either 您是否也尝试过将属性设置为静态...您正在工作的静态类不允许实例构造函数,但是您的属性也未标识为静态
public static bool EnableImages { get; set; }
public static bool CheckForUpdates { get; set; }
public static ObservableCollection<FavShowData> TvShows {get; set;}
Workaround: Move the Stuff to the ViewModel: 解决方法:将资料移至ViewModel:
public class MainWindowViewModel
{
public Settings Settings
{
get { return Settings.Instance; }
}
//....
}
And change the XAML to: 并将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>
No more Design-Time Errors. 没有更多的设计时错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.