LauncherView要回答您的问题,您的班级和您的ICustomInterface收藏之间似乎没有任何联系。@Farzi 正确地评论说,如果您希望能够从对象访问Title属性,则应该在接口中声明您的属性。ICustomInterfaceICustomInterface
要解决此问题,请将Title属性添加到ICustomInterface接口中,或者将集合的类型更改为Modules实现该Title属性的任何类型。
关于您的设置的个人想法:
就个人而言,我认为在 a 中收集UserControl对象ComboBox.ItemsSource并不是一个好主意。您将消耗所有这些资源所需的所有资源,即使只显示了一个。
与其那样做,您可以只使用string代表每个标题的 s集合,UserControl然后绑定到ComboBox.SelectedItem属性。然后,您可以只拥有一个类型的属性ICustomInterface,您可以在该SelectedItem属性更改时对其进行更新。
在 WPF 中,我们通常使用数据而不是 UI 控件。因此,一个更好的选择是操作每个视图的视图模型,并在 a 中显示视图模型ContentControl,首先设置一些DataTemplates:
<Application.Resources>
<DataTemplate DataType="{x:Type ViewModels:YourViewModel}">
<Views:YourView />
</DataTemplate>
</Application.Resources>
在主视图中:
<ContentControl Content="{Binding ViewModel}" />
在主视图模型或后面的代码中:
public ICustomInterface ViewModel
{
get { return viewModel; }
set { viewModel= value; NotifyPropertyChanged("ViewModel"); }
}
public string SelectedTitle
{
get { return selectedTitle; }
set
{
selectedTitle = value;
NotifyPropertyChanged("SelectedTitle");
if (SelectedTitle == "Something") ViewModel = new SomethingViewModel();
if (SelectedTitle == "Other") ViewModel = new OtherViewModel();
}
}
或者您可以拥有一组视图模型(浪费资源):
public string SelectedTitle
{
get { return selectedTitle; }
set
{
selectedTitle = value;
NotifyPropertyChanged("SelectedTitle");
ViewModel = ViewModelCollection.Where(v => v.Title == SelectedTitle);
}
}
我确实理解您并没有询问我的任何个人想法,我希望他们在某种程度上对您有所帮助。如果他们不受欢迎,我们深表歉意。