' Developer Express Code Central Example:
' How to prevent sorting by columns in a ListView
' 
' This example provides a workaround solution to the
' http://www.devexpress.com/scid=S131144 suggestion. Below is the list of
' implemented features:
' 1. AllowSortAttribute - this is a regular attribute class
' that can be used in your code to mark either a business class or its property,
' to specify whether you want to allow columns to be sorted in the whole class, or
' for only a specific column:
' 
' [AttributeUsage(AttributeTargets.Interface |
' AttributeTargets.Class | AttributeTargets.Field |
' AttributeTargets.Property)]
' public class AllowSortAttribute : Attribute
' {
' public AllowSortAttribute(bool allowSort) {...}
' public AllowSortAttribute()
' : this(true) { }
' ...
' 
' 
' See these examples below:
' a) This code prevents
' sorting by all columns in the
' ListView:
' 
' [DefaultClassOptions]
' [AllowSort(false)]
' public class DemoIssue :
' BaseObject {
' ...
' 
' 
' b) This code prevents sorting by the Description column
' in the ListView:
' 
' private string _Description;
' [AllowSort(false)]
' public
' string Description {
' get { return _Description; }
' set {
' SetPropertyValue("Description", ref _Description, value); }
' }
' 
' When this
' attribute is applied to a class, its value is used to calculate the default
' value of the DefaultListViewAllowSortAttribute attribute on the BOModel | Class
' node level in the application model.
' When this attribute is applied to a
' property, its value is used to calculate the default value of the AllowSort
' attribute on the BOModel | Class | Member node level in the application
' model.
' 
' 2. DefaultListViewAllowSortAttribute - this is an attribute declared
' on the BOModel | Class node level in the application model. You can use it to
' control the default sorting behavior for all ListViews of a business class. The
' value of this attribute is used to calculate the value of the attribute on the
' Views | ListView node level.
' 
' 3. AllowSort - this is an attribute declared on
' the BOModel | Member, Views | ListView and ListView | Columns | ColumnInfo nodes
' level in the application model. You can use this attribute to flexibly control
' the sorting behavior of columns.
' 
' 4. Sorting management, with the help of the
' AllowSort attribute, is supported in both Windows Forms and ASP.NET applications
' for the GridListEditor and ASPxGridListEditor correspondingly.
' 
' See
' Also:
' http://www.devexpress.com/scid=E1253
' http://www.devexpress.com/scid=E1276
' Sorting
' (ms-help://DevExpress.WindowsForms/CustomDocument3499.htm)
' Sorting
' (ms-help://DevExpress.AspNet/CustomDocument3714.htm)
' 
' You can find sample updates and versions for different programming languages here:
' http://www.devexpress.com/example=E1254

Imports Microsoft.VisualBasic
Imports System
Imports System.Collections.Generic
Imports System.Text
Imports DevExpress.Persistent.Base
Imports DevExpress.Xpo
Imports DevExpress.Persistent.BaseImpl
Imports DevExpress.ExpressApp
Imports DevExpress.Xpo.DB
Imports DevExpress.Xpo.Metadata
Imports DevExpress.Data
Imports DevExpress.ExpressApp.DC
Imports DevExpress.ExpressApp.Model


<AttributeUsage(AttributeTargets.Interface Or AttributeTargets.Class Or AttributeTargets.Field Or AttributeTargets.Property)> _
Public Class AllowSortAttribute
    Inherits Attribute
    Public Const DefaultListViewAllowSortAttributeName As String = "DefaultListViewAllowSort"
    Public Const AllowSortAttributeName As String = "AllowSort"
    Public Shared [Default] As New AllowSortAttribute(True)
    Private allowSortCore As Boolean = True
    Public Sub New()
        Me.New(True)
    End Sub
    Public Sub New(ByVal allowSort As Boolean)
        Me.allowSortCore = allowSort
    End Sub
    Public Property AllowSort() As Boolean
        Get
            Return allowSortCore
        End Get
        Set(ByVal value As Boolean)
            allowSortCore = value
        End Set
    End Property
End Class


Public MustInherit Class AllowSortListViewController
    Inherits ViewController(Of ListView)
    Protected MustOverride Sub UpdateAllowSort()
    Private Sub ListView_InfoChanged(ByVal sender As Object, ByVal e As EventArgs)
        UpdateAllowSort()
    End Sub
    Protected Function GetAllowSortListView() As Boolean
        If View.Model IsNot Nothing Then
            Return (CType(View.Model, IModelListViewAllowSort)).AllowSort
        End If
        Return False
    End Function
    Protected Function GetAllowSortColumn(ByVal column As IModelColumn) As Boolean
        If column IsNot Nothing AndAlso GetAllowSortListView() Then
            Return (CType(column, IModelColumnAllowSort)).AllowSort
        End If
        Return False
    End Function
    Protected Overloads Overrides Sub OnActivated()
        MyBase.OnActivated()
        AddHandler View.ModelChanged, AddressOf ListView_InfoChanged
    End Sub
    Protected Overloads Overrides Sub OnDeactivated()
        RemoveHandler View.ModelChanged, AddressOf ListView_InfoChanged
        MyBase.OnDeactivated()
    End Sub
End Class
