Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 17th, 2012  |  syntax: None  |  size: 1.53 KB  |  hits: 16  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. WPF binding a path to the typename of a class
  2. public class Group
  3. {
  4.     public string groupName;
  5.  
  6.     public string df_groupName
  7.     {
  8.         get { return this.groupName; }
  9.         set { this.groupName = value; }
  10.     }
  11. }
  12.  
  13. public class BasicGroup : Group
  14. {
  15.  
  16. }
  17.  
  18. public class AdvancedGroup : Group
  19. {
  20.  
  21. }
  22.  
  23. public class GroupSet
  24. {
  25.     public Group [] groups;
  26.  
  27.     public Group [] df_groups
  28.     {
  29.         get { return this.groups; }
  30.         set { this.groups = value; }
  31.     }
  32. };
  33.        
  34. <UserControl.Resources>
  35.     <ResourceDictionary>
  36.         <ObjectDataProvider x:Key="TheData" />
  37.  
  38.         <DataTemplate x:Key="GroupTemplate">
  39.             <GroupBox Header="{Binding Path=DerivedTypeNameOf(group)}">
  40.                 <TextBox Text="This is some text"/>
  41.             </GroupBox>
  42.         </DataTemplate>
  43.  
  44.     </ResourceDictionary>
  45. </UserControl.Resources>
  46.  
  47.     <ItemsControl ItemsSource="{Binding Source={StaticResource TheData}, Path=groups}" ItemTemplate="{StaticResource GroupTemplate}">
  48.         <ItemsControl.ItemsPanel>
  49.             <ItemsPanelTemplate>
  50.                 <StackPanel />
  51.             </ItemsPanelTemplate>
  52.         </ItemsControl.ItemsPanel>
  53.     </ItemsControl>
  54.        
  55. public abstract string DerivedTypeName { get; set; }
  56.        
  57. public class TypeNameConverter : IValueConverter {
  58.   public object Convert(object value, Type targetType,
  59.   object parameter, CultureInfo culture) {
  60.     return value.GetType().Name;
  61.   }
  62.  
  63.   public object ConvertBack(object value, Type targetType,
  64.   object parameter, CultureInfo culture) {
  65.     throw NotImplementedException();
  66.   }
  67. }