使用WPF,我想将GroupBox的头绑定到多态类的类型名。因此,如果我有一个名为Element的类以及两个从Element派生的类,例如BasicElement和AdvancedElement,我希望GroupBox的标题说“BasicElement”或“AdvancedElement”。这里是我用于GroupBox的xaml。它是由ItemsControl使用的DataTemplate的一部分。我希望在XAML中代替Path = DerivedTypeNameOf(group),其中group是groups数组中的每个组。WPF绑定到类的类型名称的路径
请注意,TheData的ObjectInstance被设置为GroupSet的有效实例,该实例包含一些BasicGroups和AdvancedGroups的数组。
下面是相关的代码隐藏类:
public class Group
{
public string groupName;
public string df_groupName
{
get { return this.groupName; }
set { this.groupName = value; }
}
}
public class BasicGroup : Group
{
}
public class AdvancedGroup : Group
{
}
public class GroupSet
{
public Group [] groups;
public Group [] df_groups
{
get { return this.groups; }
set { this.groups = value; }
}
};
这里的XAML:
<UserControl.Resources>
<ResourceDictionary>
<ObjectDataProvider x:Key="TheData" />
<DataTemplate x:Key="GroupTemplate">
<GroupBox Header="{Binding Path=DerivedTypeNameOf(group)}">
<TextBox Text="This is some text"/>
</GroupBox>
</DataTemplate>
</ResourceDictionary>
</UserControl.Resources>
<ItemsControl ItemsSource="{Binding Source={StaticResource TheData}, Path=groups}" ItemTemplate="{StaticResource GroupTemplate}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
你的问题是什么? – miguel 2011-05-15 16:34:45