Delegate on TabControl

5 posts, 1 answers
  1. Adam
    Adam avatar
    39 posts
    Member since:
    Jun 2009

    Posted 14 Aug 2009 Link to this post

    Is there a way to use a Delegate on the TabControl?

    Currently, I have (in XAML):
      SelectionChanged="radTabControl1_SelectionChanged"

    in the ViewModel:
            public ICommand TabSelectionChangedCommand { get; private set; } 
     
                TabSelectionChangedCommand = new DelegateCommand<object>(param => 
                { 
                    SelectionChanged(param); 
                }); 
     
            private void SelectionChanged(object obj) 
            { stuff } 

         And my otherwise empty .xaml.cs file:

            private void radTabControl1_SelectionChanged(object sender, System.Windows.RoutedEventArgs e) 
            { 
                FrameworkElement fe = (FrameworkElement)sender; 
                MyViewModel mvm = (MyViewModel) ((ObjectDataProvider)fe.DataContext).ObjectInstance; 
                if (mvm != null) 
                { 
                    mvm.TabSelectionChangedCommand.Execute(sender); 
                } 
            } 
        
     
     
    It works, but aren't we supposed to be able to do things without the code-behind?

    Or, is there a better way to bind to the SelectedIndex ?

  2. Tihomir Petkov
    Admin
    Tihomir Petkov avatar
    576 posts

    Posted 18 Aug 2009 Link to this post

    Hello Adam,

    What about two-way data binding the SelectedIndex property to a public property in your ViewModel? If the ViewModel implements the INotifyPropertyChanged interface and you add a getter and setter to the property, I believe you will be able to catch the change of the selected tab in the RadTabControl. Please try the suggested approach and let me know if it works for you.

    Regards,
    Tihomir Petkov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  3. UI for WPF is Visual Studio 2017 Ready
  4. Adam
    Adam avatar
    39 posts
    Member since:
    Jun 2009

    Posted 18 Aug 2009 Link to this post

    No, it does not.

    I tried both
    SelectedItem="{Binding CurrentState, Mode=TwoWay}"
    and
    SelectedItem="{Binding Path=CurrentState, Mode=TwoWay}"

  5. Answer
    Miroslav
    Admin
    Miroslav avatar
    922 posts

    Posted 19 Aug 2009 Link to this post

    Hi Adam,

    I created a small example with binding the SelectedItem property. It works as expected, so possibly I am missing something in your scenario.

    The CurrentState property which you bind to needs to be of the type of the item in the TabControl (RadTabItem or some ViewModel class if you are binding the TabControl). You can see if the binding is working by making the CurrentState property an object. Also, you may want to check the output window in Visual Studio which will output a message for the bindings that break.

    Hopefully this will help you,

    Greetings,
    Miroslav
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  6. Adam
    Adam avatar
    39 posts
    Member since:
    Jun 2009

    Posted 19 Aug 2009 Link to this post

    Life is easier when you bind to an object instead of an int,,,

    Thanks!
Back to Top
UI for WPF is Visual Studio 2017 Ready