WPF/Silverlight では、HTML のような TableControl にバインドすることはできますか?
-
12-09-2019 - |
質問
次のカテゴリとサブカテゴリがあるとします。
- アニメーション (ファミリー・ガイ、カルビン & ホーブス、ザ・ブーンドックス、ダック・テイルズ、ルーニー・トゥーンズ、ピンク & ザ・ブレイン)
- セサミストリート(オスカー、アーニー&バート、カーミット・デ・フロッグ、エルモ、クッキーモンスター、グローバー)
次のように行と列に(動的に)レンダリングする方法を探しています。
---------------------------------------------------- | Animations | ---------------------------------------------------- | Family Guy | Calvin & Hobes | The Boondocks | ---------------------------------------------------- | Duck Tales | Looney Toons | Pinky & The Brain | ---------------------------------------------------- | Sesame Street | ---------------------------------------------------- | Oscar | Ernie & Bert | Kermit de Frog | ---------------------------------------------------- | Elmo | Cookie Monster | Grover | ----------------------------------------------------
追伸:リスト コントロール (ItemsControl、DataGrid、ListView など) については知っていますが、どれもこの基準を満たしていないようです。
解決
何をする必要がありますすることは(あなたはすでにそれを持っているか、あなたが問合せに...グループ化を通して、あなたの階層を作成するためにLINQを使用することができますいずれか)のカテゴリーで何とかグループあなたの項目クエリを作成することです。
次に、ItemsPanelTemplateはWrapPanelまたはUniformGridに設定されたヘッダと他のItemsControlを含むItemTemplateにしてデータを表示し、クエリ結果を結合するのItemsControlを使用することができます。
あなたは以下のクラスであなたのデータを取得するために管理する(ここでは申し訳ありませんが、VBが、あなたがそれを必要とする場合はC#は遠いものからではないでしょう)と仮定します:
Public Class Category
Private _Name As String
Public Property CategoryName() As String
Get
Return _Name
End Get
Set(ByVal value As String)
_Name = value
End Set
End Property
Private _SubCategories As New List(Of SubCategory)
Public Property SubCategories() As List(Of SubCategory)
Get
Return _SubCategories
End Get
Set(ByVal value As List(Of SubCategory))
_SubCategories = value
End Set
End Property
End Class
Public Class SubCategory
Private _Name As String
Public Property SubCategoryName() As String
Get
Return _Name
End Get
Set(ByVal value As String)
_Name = value
End Set
End Property
End Class
<ItemsControl ItemsSource="{Binding QueryResult}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Border BorderThickness="1"
BorderBrush="Black">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Border BorderThickness="1"
BorderBrush="Black">
<TextBlock Margin="2"
Text="{Binding CategoryName}" />
</Border>
<ItemsControl Grid.Row="1"
ItemsSource="{Binding SubCategories}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Border BorderThickness="1"
BorderBrush="Black">
<TextBlock Margin="2"
Text="{Binding SubCategoryName}" />
</Border>
</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Columns="3" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</Grid>
</Border>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
これは非常にラフなテンプレートで、あなたが必要なものを得るために国境をいじくり回す必要があるでしょうが、それはトリックを行うだろう。
所属していません StackOverflow