Files
egtbeamwall/EgtBEAMWALL.ViewerOptimizer/Utility/EgtDataGrid.xaml.vb
T
Emmanuele Sassi 18745e3158 Completamento sviluppo gestione colonne EgtDataGrid
Impostazione tempo nesting
Impostazione Qty grezzi
2021-05-19 09:36:16 +02:00

95 lines
3.8 KiB
VB.net

Imports System.Collections.ObjectModel
Imports System.Collections.Specialized
Public Class EgtDataGrid
Sub New()
InitializeComponent()
End Sub
Public Shadows Property BindingColumns As ObservableCollection(Of String)
Get
Return CType(MyBase.GetValue(BindingColumnsProperty), ObservableCollection(Of String))
End Get
Set(value As ObservableCollection(Of String))
MyBase.SetValue(BindingColumnsProperty, value)
End Set
End Property
Public Shared ReadOnly BindingColumnsProperty As DependencyProperty = DependencyProperty.Register("BindingColumns", GetType(ObservableCollection(Of String)), GetType(EgtDataGrid), New FrameworkPropertyMetadata(New ObservableCollection(Of String), New PropertyChangedCallback(AddressOf OnDataGridColumnsPropertyChanged)) With {.BindsTwoWayByDefault = True})
Private Shared Sub OnDataGridColumnsPropertyChanged(ByVal source As DependencyObject, ByVal e As DependencyPropertyChangedEventArgs)
Dim context = TryCast(source, EgtDataGrid)
Dim oldItems = TryCast(e.OldValue, ObservableCollection(Of String))
If oldItems IsNot Nothing Then
For Index = 0 To oldItems.Count - 1
Dim Column As DataGridColumn = context.FindResource(oldItems(Index))
context.Columns.Remove(Column)
Next
RemoveHandler oldItems.CollectionChanged, AddressOf context.collectionChanged
End If
Dim newItems = TryCast(e.NewValue, ObservableCollection(Of String))
If newItems IsNot Nothing Then
For Index = 0 To newItems.Count - 1
Dim col As DataGridColumn = context.FindResource(newItems(Index))
context.Columns.Add(col)
Next
AddHandler newItems.CollectionChanged, AddressOf context.collectionChanged
End If
End Sub
Private Sub collectionChanged(ByVal sender As Object, ByVal e As NotifyCollectionChangedEventArgs)
Select Case e.Action
Case NotifyCollectionChangedAction.Add
If e.NewItems IsNot Nothing Then
For Each one As String In e.NewItems
Dim col As DataGridColumn = CurrDataGrid.FindResource(one)
CurrDataGrid.Columns.Insert(e.NewStartingIndex, col)
If col.DisplayIndex <> e.NewStartingIndex Then
CurrDataGrid.Columns(e.NewStartingIndex).DisplayIndex = e.NewStartingIndex
End If
Next
End If
Case NotifyCollectionChangedAction.Remove
If e.OldItems IsNot Nothing Then
For Each one As String In e.OldItems
Dim col As DataGridColumn = CurrDataGrid.FindResource(one)
CurrDataGrid.Columns.Remove(col)
Next
End If
Case NotifyCollectionChangedAction.Move
'Dim col As DataGridColumn = CurrDataGrid.FindResource(BindingColumns(e.OldStartingIndex))
'col.DisplayIndex = e.NewStartingIndex
CurrDataGrid.Columns.Move(e.OldStartingIndex, e.NewStartingIndex)
CurrDataGrid.Columns(e.NewStartingIndex).DisplayIndex = e.NewStartingIndex
'Dim x = CurrDataGrid.Columns(e.OldStartingIndex).DisplayIndex
Case NotifyCollectionChangedAction.Reset
CurrDataGrid.Columns.Clear()
If e.NewItems IsNot Nothing Then
For Each one As DataGridColumn In e.NewItems
Dim col As DataGridColumn = CurrDataGrid.FindResource(one)
CurrDataGrid.Columns.Add(col)
Next
End If
Case NotifyCollectionChangedAction.Replace
Dim col As DataGridColumn = CurrDataGrid.FindResource(e.NewItems(0))
CurrDataGrid.Columns(e.NewStartingIndex) = col
End Select
End Sub
End Class