目标:
我想实现这样的事情在WPF:
(来源: wordpress.org )
初步的解决方案:
目前,我正在尝试使用ItemsControl
与ItemTemplate
的组成的Expander
。
我希望有一个一致的外观为Header
的部分Expander
,但我想要的Content
的部分Expander
是完全灵活的。 因此,它基本上是一组垂直堆叠的“门户”,其中每个portlet具有一致的标题栏,但不同的内容。
到目前为止的代码:
这是我的时刻:
<ItemsControl
Grid.Row="2"
Grid.Column="2">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Expander>
<Expander.HeaderTemplate>
<DataTemplate>
<StackPanel
Orientation="Horizontal">
<TextBlock
FontSize="14"
FontWeight="Bold"
Text="Title_Of_Expander_Goes_Here" />
<TextBlock
Margin="10,0,0,0"
FontWeight="Bold"
FontSize="18"
Text="*" />
</StackPanel>
</DataTemplate>
</Expander.HeaderTemplate>
<Expander.Template>
<ControlTemplate
TargetType="Expander">
<Border
BorderThickness="1">
<ContentPresenter />
</Border>
</ControlTemplate>
</Expander.Template>
</Expander>
</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsControl.Items>
<StackPanel>
<TextBlock
FontSize="14"
FontWeight="Bold"
Text="Users:" />
<wt:DataGrid
Margin="0,1,0,0"
AutoGenerateColumns="False"
CanUserAddRows="True"
CanUserDeleteRows="True"
ItemsSource="{Binding Source={StaticResource Main_SystemUsers}, XPath=//Users/*}">
<wt:DataGrid.Columns>
<wt:DataGridTextColumn
Header="User Name"
Binding="{Binding XPath=@UserName}" />
<wt:DataGridComboBoxColumn
Header="Role"
ItemsSource="{Binding Source={StaticResource Main_UserRoles}, XPath=//Roles/*}"
SelectedValueBinding="{Binding XPath=@Role}" />
</wt:DataGrid.Columns>
</wt:DataGrid>
<StackPanel
Margin="0,10,0,0"
Orientation="Horizontal">
<Button
Content="Add New User..." />
<Button
Margin="10,0,0,0"
Content="Delete User..." />
</StackPanel>
</StackPanel>
</ItemsControl.Items>
</ItemsControl>
讨论:
这显示,当我运行,这是唯一DataGrid
用户和按钮(“添加新用户”和“删除用户”)在其下方。 没有Expander
或标题栏。 此外,即使我没有看到一个,我不知道如何建立一个Binding
的标题栏上显示的标题。 我知道,如果我用怎么办绑定ItemsSource
,但我想声明我的设置项目。
问题:
我应该如何去吗? 我在寻找无论是什么,我现在有一个修复或清洁片解决方案。
编辑:
我落得这样做是更换ItemsControl
与StackPanel
,只是写我的扩展的样式。 这被证明是简单得多,而且真的是无益处ItemsControl
,因为我反正需要申报为每个项目的自定义内容。 剩下的一个问题是如何实现每个扩展自定义标题。 这就是@Thomas莱维斯克的使用建议TemplateBinding
进来了。我所要做的就是替换Text="Title_Of_Expander_Goes_Here"
在我的头的模板(见上面的代码)与Text="{TemplateBinding Content}"
。