Quantcast
Channel: Extended WPF Toolkit™ Community Edition
Viewing all articles
Browse latest Browse all 4964

New Post: Pass property changes in ObservableCollection back to CollectionControl

$
0
0
I managed to get the desired effect using converters. There is probably a much more concise and elegant solution but for anyone who is interested:

XAML:
<Window x:Class="MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:con="clr-namespace:Andrews_Collection_Editor.nsConverters"
    Title="MainWindow" Height="800" Width="525" xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit">
    <Window.Resources>
        <con:clsCCSelectedItemConverter  x:Key = "myCCSelectedItemConverter"/>
    </Window.Resources>
    <Grid>
        <StackPanel>
            <xctk:CollectionControl Height="240"
                                HorizontalAlignment="Left"
                                Name="CollectionControlElements"
                                VerticalAlignment="Top"
                                Width="479"
                                >
                

                    </xctk:CollectionControl>

            <xctk:CollectionControl Height="240"
                                HorizontalAlignment="Left"
                                Name="CollectionControlLayers"
                                VerticalAlignment="Top"
                                Width="479"

                                    ItemsSource="{Binding Path=SelectedItem, Converter={StaticResource myCCSelectedItemConverter}}"
                                >


                <xctk:CollectionControl.Style>
                <Style TargetType="xctk:CollectionControl">
                    <Style.Resources>
                        <Style TargetType="ListBox">
                            <Setter Property="ItemTemplate">
                                <Setter.Value>
                                    <DataTemplate>
                                        <TextBlock Text="{Binding DisplayedName}"
                                            Background="LightGreen" />
                                    </DataTemplate>
                                </Setter.Value>
                            </Setter>
                        </Style>
                    </Style.Resources>
                </Style>
            </xctk:CollectionControl.Style>

           
        </xctk:CollectionControl>

        </StackPanel>
    </Grid>
</Window>
which uses the following converter:
Namespace nsConverters
    Public Class clsCCSelectedItemConverter

        Implements System.Windows.Data.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

            'First clear all items (layers) from the Layers CollectionControl
            If Not IsNothing(myWindow1) Then
                myWindow1.CollectionControlLayers.Items.Clear()
            End If

            'Return if no value is selected....this happens when a construction is deleted
            If IsNothing(value) Then
                Return -1
            End If

            'Otherwise Add the SelectedItem construction layers to the Layers CollectionControl.
            Return value.LayerCollection

        End Function

        Public Function ConvertBack(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.ConvertBack

            'Dummy function - does nothing but ConvertBack must be present.
            Return value

        End Function

    End Class

End Namespace
Code behind:
Imports System.Collections.ObjectModel


Class MainWindow
    Public myConstruction As New clsConstruction

    Public myConstructionCollection As New ObservableCollection(Of clsConstruction)

    '    Public myWindow1 As New MainWindow
    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()


        myConstruction = New clsConstruction
        myConstructionCollection.Add(myConstruction)

        CollectionControlElements.DataContext = myConstructionCollection
        CollectionControlElements.ItemsSource = myConstructionCollection
        CollectionControlElements.NewItemTypes = New ObservableCollection(Of System.Type)() From {GetType(clsConstruction)}

        CollectionControlLayers.DataContext = CollectionControlElements 'myConstructionCollection(0)

        CollectionControlLayers.NewItemTypes = New ObservableCollection(Of System.Type)() From {GetType(clsLayer)}


    End Sub


    Private Sub CollectionControlElements_ItemAdded(sender As Object, e As Xceed.Wpf.Toolkit.ItemEventArgs) Handles CollectionControlElements.ItemAdded
        CollectionControlElements.PersistChanges()
    End Sub


    Private Sub CollectionControlLayers_ItemAdded(sender As Object, e As Xceed.Wpf.Toolkit.ItemEventArgs) Handles CollectionControlLayers.ItemAdded
        CollectionControlLayers.PersistChanges()
    End Sub

    Private Sub MainWindow_Loaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded
        myWindow1 = Me
    End Sub

    Private Sub CollectionControlElements_ItemDeleted(sender As Object, e As Xceed.Wpf.Toolkit.ItemEventArgs) Handles CollectionControlElements.ItemDeleted
        CollectionControlElements.PersistChanges()
    End Sub

    Private Sub CollectionControlLayers_ItemDeleted(sender As Object, e As Xceed.Wpf.Toolkit.ItemEventArgs) Handles CollectionControlLayers.ItemDeleted
        CollectionControlLayers.PersistChanges()
    End Sub
End Class
where
Imports System.ComponentModel
Imports System.Collections.ObjectModel

Public Class clsLayer



    Implements INotifyPropertyChanged

    Public myThickness As Double
    Public myDisplayedName As String

    Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged

    Public Sub OnPropertyChanged(ByVal e As PropertyChangedEventArgs)
        If Not PropertyChangedEvent Is Nothing Then
            RaiseEvent PropertyChanged(Me, e)
        End If
    End Sub

    Public Sub New()
        myThickness = 26.0
        myDisplayedName = "New Layer"
    End Sub

    Public Property DisplayedName As String

        Get
            Return myDisplayedName
        End Get
        Set(value As String)
            myDisplayedName = value
            OnPropertyChanged(New PropertyChangedEventArgs("DisplayedName"))
        End Set
    End Property

    Public Property Thickness() As Double
        Get
            Return myThickness
        End Get
        Set(ByVal value As Double)
            myThickness = value
            OnPropertyChanged(New PropertyChangedEventArgs("Thickness"))
            myWindow1.CollectionControlLayers.PersistChanges()
            myWindow1.CollectionControlElements.PersistChanges()
        End Set
    End Property
End Class
and
Imports System.ComponentModel
Imports System.Collections.ObjectModel
Public Class clsConstruction
    Implements INotifyPropertyChanged

    Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged

    Public Sub OnPropertyChanged(ByVal e As PropertyChangedEventArgs)
        If Not PropertyChangedEvent Is Nothing Then
            RaiseEvent PropertyChanged(Me, e)
        End If
    End Sub

    Public myLayerCollection As New ObservableCollection(Of clsLayer)
    Public myDescription As String
    Public myTotalThickness As Double
    Private LayerCollectionThickness As Double = 0.0

    Public Sub New()
        myDescription = "New Construction"
        myLayerCollection.Add(New clsLayer)
        AddHandler myLayerCollection.CollectionChanged, AddressOf myLayerCollection_CollectionChanged
    End Sub

    Public Sub myLayerCollection_CollectionChanged(ByVal sender As Object, ByVal e As System.Collections.Specialized.NotifyCollectionChangedEventArgs)
        If e.Action = System.Collections.Specialized.NotifyCollectionChangedAction.Reset Then
            LayerCollectionThickness = 0.0
        Else
            LayerCollectionThickness += DirectCast(e.NewItems(0), clsLayer).Thickness
            OnPropertyChanged(New PropertyChangedEventArgs("TotalThickness"))
        End If
    End Sub

    <Browsable(False)>
    Public Property LayerCollection As ObservableCollection(Of clsLayer)

        Get
            Return myLayerCollection
        End Get
        Set(value As ObservableCollection(Of clsLayer))
            myLayerCollection = value
            OnPropertyChanged(New PropertyChangedEventArgs("LayerCollection"))
            OnPropertyChanged(New PropertyChangedEventArgs("TotalThickness"))
        End Set
    End Property

    Public Property Description As String

        Get
            Return myDescription
        End Get
        Set(value As String)
            myDescription = value
            OnPropertyChanged(New PropertyChangedEventArgs("Description"))
        End Set
    End Property

    ReadOnly Property TotalThickness() As Double
        Get
            myTotalThickness = 0.0
            If Not IsNothing(LayerCollection) Then
                If LayerCollection.Count > 0 Then
                    For i = 0 To LayerCollection.Count - 1

                        myTotalThickness = myTotalThickness + LayerCollection(i).Thickness
                    Next
                End If
                Return Math.Round(myTotalThickness, 3)
            End If
        End Get
    End Property

   
End Class

Viewing all articles
Browse latest Browse all 4964

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>