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()
{
}
}