使用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