c# – WPF中的DataTemplates

前端之家收集整理的这篇文章主要介绍了c# – WPF中的DataTemplates前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我对 WPF中的数据模板有一般性问题.假设我有一个名为“问题”的抽象类,以及各种子类,如“MathQuestion”,“GeographyQuestion”等.在某些情况下,使用“问题”数据模板将问题呈现为“问题”已经足够了,但是假设我有一个不同子类的随机问题对象列表,我想反过来显示它们.我想使用他们的特定数据模板而不是他们的通用问题数据模板将它们显示用户,但由于我不知道在设计时,无论如何都告诉WPF,“嘿,这是一个Quesitons列表,但是使用反射来弄清楚他们的具体类型并使用那个数据模板?“

到目前为止我所想到的:我认为除了我的问题集之外,我可以使用反射创建另一个特定类型的集合,并以某种方式将其绑定到“blah”,然后我会得到所需的效果,但是你只能绑定到WPF中的DependencyProperties,所以我不确定我绑定到什么.我真的不喜欢这个想法,我的直觉告诉我有一个更优雅的方法解决这个问题.

我不是在寻找具体的代码,只是一个完成我想要做的事情的一般策略.另外,如果有帮助的话,我大部分时间都在使用MVVM.

谢谢

解决方法

我认为这样的东西应该是开箱即用的:
  1. <UserControl.Resources>
  2. <DataTemplate DataType="{x:Type vm:GenericQuestionviewmodel}">
  3. <v:GenericQuestion/>
  4. </DataTemplate>
  5. <DataTemplate DataType="{x:Type tvm:GeographyQuestionviewmodel}">
  6. <tv:GeographyQuestion/>
  7. </DataTemplate>
  8. <DataTemplate DataType="{x:Type tvm:BiologyQuestionviewmodel}">
  9. <tv:BiologyQuestion/>
  10. </DataTemplate>
  11. </UserControl.Resources>
  12.  
  13. <ContentControl Content="{Binding Questionviewmodel}">

编辑:

是的,这绝对应该有效.这是一个更完整的例子:

主视图模型

  1. public class MainWindowviewmodel : viewmodelBase
  2. {
  3. public ObservableCollection<Questionviewmodel> Questionviewmodels { get; set; }
  4.  
  5. public MainWindowviewmodel()
  6. {
  7. Questionviewmodels = new ObservableCollection<Questionviewmodel>
  8. {
  9. new GenericQuestionviewmodel(),new GeographyQuestionviewmodel(),new BiologyQuestionviewmodel()
  10. };
  11. }
  12. }

问题查看模型

  1. public abstract class Questionviewmodel : viewmodelBase
  2. {
  3. }
  4.  
  5. public class GenericQuestionviewmodel : Questionviewmodel
  6. {
  7. }
  8.  
  9. public class GeographyQuestionviewmodel : Questionviewmodel
  10. {
  11. }
  12.  
  13. public class BiologyQuestionviewmodel : Questionviewmodel
  14. {
  15. }

问题用户控制

  1. <UserControl x:Class="WpfApplication1.GenericQuestion" ...>
  2. <Grid>
  3. <TextBlock Text="Generic Question" />
  4. </Grid>
  5. </UserControl>
  6.  
  7. <UserControl x:Class="WpfApplication1.GeographyQuestion" ...>
  8. <Grid>
  9. <TextBlock Text="Geography Question" />
  10. </Grid>
  11. </UserControl>
  12.  
  13. <UserControl x:Class="WpfApplication1.BiologyQuestion" ...>
  14. <Grid>
  15. <TextBlock Text="Biology Question" />
  16. </Grid>
  17. </UserControl>

主窗口

  1. <Window x:Class="WpfApplication1.MainWindow" ...
  2. Title="MainWindow"
  3. Height="900"
  4. Width="525">
  5. <Window.DataContext>
  6. <local:MainWindowviewmodel />
  7. </Window.DataContext>
  8. <Window.Resources>
  9. <DataTemplate DataType="{x:Type local:GenericQuestionviewmodel}">
  10. <local:GenericQuestion />
  11. </DataTemplate>
  12. <DataTemplate DataType="{x:Type local:GeographyQuestionviewmodel}">
  13. <local:GeographyQuestion />
  14. </DataTemplate>
  15. <DataTemplate DataType="{x:Type local:BiologyQuestionviewmodel}">
  16. <local:BiologyQuestion />
  17. </DataTemplate>
  18. </Window.Resources>
  19. <ItemsControl ItemsSource="{Binding Questionviewmodels}">
  20. <ItemsControl.ItemTemplate>
  21. <DataTemplate>
  22. <ContentControl Content="{Binding}" />
  23. </DataTemplate>
  24. </ItemsControl.ItemTemplate>
  25. </ItemsControl>
  26. </Window>

更新

Kyle Tolle指出了一个很好的简化设置ItemsControl.ItemTemplate.这是结果代码

  1. <ItemsControl ItemsSource="{Binding Questionviewmodels}"
  2. ItemTemplate="{Binding}" />

猜你在找的C#相关文章