目標:如何在WPF中製作「Accordion Widget」?
我想實現WPF是這樣的:
初步的解決方案:
此刻,我試圖使用ItemsControl
和ItemTemplate
組成的Expander
。
我希望有一個一致的外觀爲Expander
的Header
部分,但我想Expander
的Content
部分是完全靈活的。所以,它基本上是一組垂直堆疊的「portlet」,其中每個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 [email protected]}" />
<wt:DataGridComboBoxColumn
Header="Role"
ItemsSource="{Binding Source={StaticResource Main_UserRoles}, XPath=//Roles/*}"
SelectedValueBinding="{Binding [email protected]}" />
</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
,我知道如何進行綁定,但我想以聲明方式設置項目。
問題:
我應該如何去了解呢?我正在尋找我現在擁有的解決方案或者是一張清潔解決方案。
編輯:
我最終什麼事做用StackPanel
更換ItemsControl
,只是寫我的擴展的樣式。事實證明這很簡單,因爲我需要爲每個項目聲明自定義內容,所以ItemsControl
確實沒有任何好處。剩下的一個問題是如何爲每個擴展器實現自定義標題。這就是@托馬斯列維斯克的建議使用TemplateBinding
進來。我所要做的只是在我的標題的模板(見上面的代碼)Text="{TemplateBinding Content}"
替換Text="Title_Of_Expander_Goes_Here"
。
'TemplateBinding'!這就是我需要的。謝謝:) – devuxer 2009-09-25 01:02:43