我使用WPF MVVM并在我的TabControll中使用Binding(XAML):
<TabControl SelectedIndex="{Binding SelectedTabIndex, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, IsAsync=True}"
My Property看起来像这样:
private int _selectedTabIndex;
public int SelectedTabIndex
{
get { return _selectedTabIndex; }
set
{
if (TestbenchValidationCheck == true)
{
_selectedTabIndex = value;
OnPropertyChanged("SelectedTabIndex");
}
else
{
_selectedTabIndex = 0;
OnPropertyChanged("SelectedTabIndex");
}
}
}
如果我切换TabControll的标签,那么我在标签中看不到任何内容...如果我更改次数并且运气好,我可以看到标签内容。问题是什么?
供参考: 如果我删除“IsAsync = true”属性(在XAML代码中),那么我无法切换选项卡,我不知道为什么。请注意:如果有其他解决方案,我真的不想使用IsAsync属性。
这是我的OnPropertyChanged处理程序:
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
var handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
这是我的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()
{
}
}