Back to Devexpress

How to: Prevent the Tab Items from being Selected

wpf-8247-controls-and-libraries-layout-management-tab-control-examples-how-to-prevent-the-tab-items-from-being-selected.md

latest8.4 KB
Original Source

How to: Prevent the Tab Items from being Selected

  • Jun 07, 2019
  • 4 minutes to read

The following example handles the DXTabControl.SelectionChanging event and does not allow users to select tab items if their IsUrgent property is set to false.

xaml
<Window xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        x:Class="DXTabControl_RestrictingSelection.MainWindow" 
        Title="MainWindow" Height="350" Width="525">
    <Grid x:Name="LayoutRoot" Loaded="LayoutRoot_Loaded">
        <dx:DXTabControl x:Name="tabControl" Height="140" Width="300" 
                         SelectionChanging="tabControl_SelectionChanging">
            <dx:DXTabControl.ItemHeaderTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Path=ProductName}" />
                </DataTemplate>
            </dx:DXTabControl.ItemHeaderTemplate>
            <dx:DXTabControl.ItemTemplate>
                <DataTemplate>
                    <StackPanel>
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Text="ID = " />
                            <TextBlock Text="{Binding Path=ID}" Foreground="Blue" />
                        </StackPanel>
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Text="Price = " />
                            <TextBlock Text="{Binding Path=Price}" Foreground="Blue" />
                        </StackPanel>
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Text="Discount = " />
                            <TextBlock Text="{Binding Path=Discount}" Foreground="Blue" />
                        </StackPanel>
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Text="Order Date = " />
                            <TextBlock Text="{Binding Path=OrderDate}" Foreground="Blue" />
                        </StackPanel>
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Text="Status = " />
                            <TextBlock Text="{Binding Path=Status}" Foreground="Blue" />
                        </StackPanel>
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Text="Urgent = " />
                            <TextBlock Text="{Binding Path=IsUrgent}" Foreground="Blue" />
                        </StackPanel>
                    </StackPanel>
                </DataTemplate>
            </dx:DXTabControl.ItemTemplate>
            <dx:DXTabControl.View>
                <dx:TabControlScrollView ShowHeaderMenu="True" />
            </dx:DXTabControl.View>
        </dx:DXTabControl>
    </Grid>
</Window>
csharp
using System.Windows;
using DevExpress.Xpf.Core;
using DXExample.DemoData;

namespace DXTabControl_RestrictingSelection {
    public partial class MainWindow : Window {
        public MainWindow() {
            InitializeComponent();
        }
        private void LayoutRoot_Loaded(object sender, RoutedEventArgs e) {
            tabControl.ItemsSource = Invoice.GetData();
        }
        private void tabControl_SelectionChanging(object sender,
            TabControlSelectionChangingEventArgs e) {
            if (!((Invoice)tabControl.Items[e.NewSelectedIndex]).IsUrgent)
                e.Cancel = true;
        }
    }
}
vb
Imports System.Windows
Imports DevExpress.Xpf.Core
Imports DXExample.DemoData

Namespace DXTabControl_RestrictingSelection
    Partial Public Class MainWindow
        Inherits Window

        Public Sub New()
            InitializeComponent()
        End Sub
        Private Sub LayoutRoot_Loaded(ByVal sender As Object, ByVal e As RoutedEventArgs)
            tabControl.ItemsSource = Invoice.GetData()
        End Sub
        Private Sub tabControl_SelectionChanging(ByVal sender As Object, ByVal e As TabControlSelectionChangingEventArgs)
            If Not CType(tabControl.Items(e.NewSelectedIndex), Invoice).IsUrgent Then
                e.Cancel = True
            End If
        End Sub
    End Class
End Namespace
csharp
using System;
using System.Collections.Generic;

namespace DXExample.DemoData {
    public enum InvoiceStatus { Ordered, Payed, Shipped, Delivered, Invalidated }
    public class Invoice {
        public int ID { get; set; }
        public string ProductName { get; set; }
        public double Price { get; set; }
        public DateTime OrderDate { get; set; }
        public double Discount { get; set; }
        public bool IsUrgent { get; set; }
        public InvoiceStatus Status { get; set; }
        static public List<Invoice> GetData() {
            List<Invoice> data = new List<Invoice>();

            data.Add(new Invoice() { ID = 0, ProductName = "Tofu", IsUrgent = false,
                Price = 235.54, Discount = 9.4, Status = InvoiceStatus.Invalidated,
                OrderDate = new DateTime(2009, 3, 12) });
            data.Add(new Invoice() { ID = 1, ProductName = "Ravioli Angelo", IsUrgent = true,
                Price = 178.45, Discount = 6.1, Status = InvoiceStatus.Delivered,
                OrderDate = new DateTime(2009, 2, 9) });
            data.Add(new Invoice() { ID = 2, ProductName = "Geitost", IsUrgent = false,
                Price = 89.98, Discount = 5.4, Status = InvoiceStatus.Payed,
                OrderDate = new DateTime(2009, 4, 1) });
            data.Add(new Invoice() { ID = 3, ProductName = "Chang", IsUrgent = true,
                Price = 189.33, Discount = 18.2, Status = InvoiceStatus.Shipped,
                OrderDate = new DateTime(2009, 5, 23) });
            data.Add(new Invoice() { ID = 4, ProductName = "Inlagd Sill", IsUrgent = false, 
                Price = 509.10, Discount = 22.2, Status = InvoiceStatus.Ordered,
                OrderDate = new DateTime(2009, 6, 30) });
            data.Add(new Invoice() { ID = 5, ProductName = "Alice Mutton", IsUrgent = true,
                Price = 791.18, Discount = 24.4, Status = InvoiceStatus.Invalidated,
                OrderDate = new DateTime(2009, 5, 7) });

            return data;
        }
    }
}
vb
Imports System
Imports System.Collections.Generic

Namespace DXExample.DemoData
    Public Enum InvoiceStatus
        Ordered
        Payed
        Shipped
        Delivered
        Invalidated
    End Enum
    Public Class Invoice
        Public Property ID() As Integer
        Public Property ProductName() As String
        Public Property Price() As Double
        Public Property OrderDate() As Date
        Public Property Discount() As Double
        Public Property IsUrgent() As Boolean
        Public Property Status() As InvoiceStatus
        Public Shared Function GetData() As List(Of Invoice)
            Dim data As New List(Of Invoice)()

            data.Add(New Invoice() With {.ID = 0, .ProductName = "Tofu", .IsUrgent = False, .Price = 235.54, .Discount = 9.4, .Status = InvoiceStatus.Invalidated, .OrderDate = New Date(2009, 3, 12)})
            data.Add(New Invoice() With {.ID = 1, .ProductName = "Ravioli Angelo", .IsUrgent = True, .Price = 178.45, .Discount = 6.1, .Status = InvoiceStatus.Delivered, .OrderDate = New Date(2009, 2, 9)})
            data.Add(New Invoice() With {.ID = 2, .ProductName = "Geitost", .IsUrgent = False, .Price = 89.98, .Discount = 5.4, .Status = InvoiceStatus.Payed, .OrderDate = New Date(2009, 4, 1)})
            data.Add(New Invoice() With {.ID = 3, .ProductName = "Chang", .IsUrgent = True, .Price = 189.33, .Discount = 18.2, .Status = InvoiceStatus.Shipped, .OrderDate = New Date(2009, 5, 23)})
            data.Add(New Invoice() With {.ID = 4, .ProductName = "Inlagd Sill", .IsUrgent = False, .Price = 509.10, .Discount = 22.2, .Status = InvoiceStatus.Ordered, .OrderDate = New Date(2009, 6, 30)})
            data.Add(New Invoice() With {.ID = 5, .ProductName = "Alice Mutton", .IsUrgent = True, .Price = 791.18, .Discount = 24.4, .Status = InvoiceStatus.Invalidated, .OrderDate = New Date(2009, 5, 7)})

            Return data
        End Function
    End Class
End Namespace