Results 1 to 15 of 15

Thread: [RESOLVED] How to prevent a TabItem from being selected

Threaded View

  1. #1

    Thread Starter
    Addicted Member Kram Kramer's Avatar
    Join Date
    Dec 2016
    Posts
    131

    Resolved [RESOLVED] How to prevent a TabItem from being selected

    There is no TabControl.Selecting Event in the WPF while there is in the Winform as you can see following.
    https://msdn.microsoft.com/en-us/lib...v=vs.110).aspx


    I have found following source for this issue.
    https://joshsmithonwpf.wordpress.com...eing-selected/

    Code:
    public Window1()
    {
         InitializeComponent();
    
         base.DataContext = new DataSource();
    
         var items = new string[] { "A", "B", "C" };
         this.tab.ItemsSource = items;
         var collView = CollectionViewSource.GetDefaultView(items);
         collView.CurrentChanging += this.OnTabItemSelecting;
    }
    
    void OnTabItemSelecting(object sender, CurrentChangingEventArgs e)
    {
         bool allow = this.chk.IsChecked.GetValueOrDefault();
         if (!allow)
         {
              // SelectedContent hasn't changed yet, so use it to figure out
              // the index of the previously selected tab
              int prevIdx = this.tab.Items.IndexOf(this.tab.SelectedContent);
              this.tab.SelectedIndex = prevIdx;
         }
    }

    Converted code via http://converter.telerik.com/;

    Code:
    Public Sub Window1()
        InitializeComponent()
        MyBase.DataContext = New DataSource()
        Dim items = New String() {"A", "B", "C"}
        Me.tab.ItemsSource = items
        Dim collView = CollectionViewSource.GetDefaultView(items)
        collView.CurrentChanging += AddressOf Me.OnTabItemSelecting
    End Sub
    
    Private Sub OnTabItemSelecting(ByVal sender As Object, ByVal e As CurrentChangingEventArgs)
        Dim allow As Boolean = Me.chk.IsChecked.GetValueOrDefault()
        If Not allow Then
            Dim prevIdx As Integer = Me.tab.Items.IndexOf(Me.tab.SelectedContent)
            Me.tab.SelectedIndex = prevIdx
        End If
    End Sub
    But I have no idea how that vb.net code works.

    So, please somebody explain me how that vb.net code works in WPF.
    Last edited by Kram Kramer; Feb 26th, 2018 at 02:19 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width