0

I use WPF MVVM and have a Binding in my TabControll like this (XAML):

<TabControl SelectedIndex="{Binding SelectedTabIndex, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, IsAsync=True}"

My Property looks like that:

private int _selectedTabIndex;
public int SelectedTabIndex
{
    get { return _selectedTabIndex; }
    set
    {
        if (TestbenchValidationCheck == true)
        {
            _selectedTabIndex = value;
            OnPropertyChanged("SelectedTabIndex");
        }
        else
        {
            _selectedTabIndex = 0;
            OnPropertyChanged("SelectedTabIndex");
        }
    }
}

If I switch the tab of the TabControll, then I don't see any content in the tab... if I change it more times and have luck, than I can see the tab content. What is the problem?

FYI: If I delete the "IsAsync=true" attribute (in XAML code), then I am not able to switch the tab, i don't know why. Please note: I dont really want to use the IsAsync attribute, if there is another solution.

This is my OnPropertyChanged handler:

 public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            var handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        }

This is my Codebehind:

    public partial class MainWindow : Window
{


    public MainWindow(object obj)
    {
        DataContext = obj;
        InitializeComponent();
        SetBindingForCBXLists();
    }

    private void SetBindingForCBXLists()
    {
        var binding = new Binding();
        binding.Path = new PropertyPath("Element");
        binding.Source = DataContext;  
        BindingOperations.SetBinding(PortListBox, DataGridComboBoxColumn.ItemsSourceProperty, binding);

        var binding2 = new Binding();
        binding2.Path = new PropertyPath("Psc.CanTypes.Element");
        binding2.Source = DataContext;  
        BindingOperations.SetBinding(TypeListBox, DataGridComboBoxColumn.ItemsSourceProperty, binding2);
    }

    public MainWindow()
    {

    }
}
Fardey
  • 5
  • 3
  • Can you verify the problem is not related to your `TestbenchValidationCheck`? Also, from the `IsAsync` [MSDN page](https://msdn.microsoft.com/en-us/library/system.windows.data.binding.isasync(v=vs.110).aspx): "Use the IsAsync property when the get accessor of your binding source property might take a long time.", so you don't need to add that. Also could you confirm that your datacontext is set correctly. – Keyur PATEL Jul 25 '17 at 07:56
  • I have to check the boolean variable before. If the variable is true, than it should switch the tab, if not, than it should not switch. – Fardey Jul 25 '17 at 08:09
  • Then you'd just need a single if statement, without the else. Try something like `if (TestbenchValidationCheck == true) _selectedTabIndex = value; OnPropertyChanged("SelectedTabIndex");` – Keyur PATEL Jul 25 '17 at 08:11
  • the result is the same. i cant see the content of the tab if i switch the tab and my Checkvariable is true. if it is false, then i cant switch (but this is a correct working) – Fardey Jul 25 '17 at 08:15
  • @Fardey Do you see the content without binding for SelectedTabIndex? – Rekshino Jul 25 '17 at 08:18
  • @Rekshino: yes I do. – Fardey Jul 25 '17 at 08:22
  • I think this is a synchronizing problem... but is there not a solution without the Async attribute? – Fardey Jul 25 '17 at 08:23
  • You have to provide more code. The code you have posted do work. – Rekshino Jul 25 '17 at 08:28
  • @Rekshino: I have provide my Codebehind also. – Fardey Jul 25 '17 at 08:43
  • 1
    What is your XAML. Why you don't set the bindings in XAML, but in code behind? What is content of TabControl? You need to provide a good Minimal, Complete, and Verifiable example that reliably reproduces your problem. – Rekshino Jul 25 '17 at 09:00

0 Answers0