WPF:Formattazione condizionale della visualizzazione struttura dell'organigramma
Domanda
L'azienda ha la tradizionale struttura organizzativa complessa, che definisce la quantità di livelli utilizzando la lettera "n" anziché un numero effettivo.Cercherò di esprimere la struttura che sto cercando di ottenere con un carattere a spaziatura fissa:
Alice
,--------|-------,------,------,
Bob Fred Jack Kim Lucy
| |
Charlie Greg
Darren Henry
Eric
Come puoi vedere non è simmetrico, poiché Jack, Kim e Lucy fanno rapporto ad Alice ma non hanno rapporti propri.
Usare un TreeView
con un ItemsPanel
contenente a StackPanel
E Orientation="Horizontal"
È abbastanza facile, ma ciò può comportare un risultato molto grande TreeView
una volta che alcune persone ne hanno altre 20 che riferiscono a loro!Puoi anche usare Triggers
per sbirciare se a TreeViewItem
ha figli con Property="TreeViewItem.HasItems"
, ma questo non è nello stesso contesto di quello menzionato prima ItemsPanel
. Per esempio:Posso dire che Fred ha dei rapporti, ma non se hanno dei rapporti propri.
Quindi, puoi formattare in modo condizionale TreeViewItems
essere Verticali se non hanno figli propri?
Soluzione 2
Alla fine ho utilizzato i suggerimenti dell'articolo collegato, che avevo già letto ma che non pensavo mi avrebbero aiutato.
Il succo avviene qui, in un convertitore:
<ValueConversion(GetType(ItemsPresenter), GetType(Orientation))> _
Public Class ItemsPanelOrientationConverter
Implements IValueConverter
Public Function Convert(ByVal value As Object, ByVal targetType As System.Type, _
ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) _
As Object Implements System.Windows.Data.IValueConverter.Convert
'The 'value' argument should reference an ItemsPresenter.'
Dim itemsPresenter As ItemsPresenter = TryCast(value, ItemsPresenter)
If itemsPresenter Is Nothing Then
Return Binding.DoNothing
End If
'The ItemsPresenter''s templated parent should be a TreeViewItem.'
Dim item As TreeViewItem = TryCast(itemsPresenter.TemplatedParent, TreeViewItem)
If item Is Nothing Then
Return Binding.DoNothing
End If
For Each i As Object In item.Items
Dim element As StaffMember = TryCast(i, StaffMember)
If element.IsManager Then
'If this element has children, then return Horizontal'
Return Orientation.Horizontal
End If
Next
'Must be a stub ItemPresenter'
Return Orientation.Vertical
End Function
Che a sua volta viene consumato in uno stile che ho creato per TreeView:
<Setter Property="ItemsPanel">
<Setter.Value>
<ItemsPanelTemplate >
<ItemsPanelTemplate.Resources>
<local:ItemsPanelOrientationConverter x:Key="conv" />
</ItemsPanelTemplate.Resources>
<StackPanel IsItemsHost="True"
Orientation="{Binding
RelativeSource={x:Static RelativeSource.TemplatedParent},
Converter={StaticResource conv}}" />
</ItemsPanelTemplate>
</Setter.Value>
</Setter>
Altri suggerimenti
Josh Smith ha un eccellente articolo di CodeProject su TreeView.Leggilo Qui