TabItem as any Selector item has the IsSelected property. You may try to bind it with view model using two-way binding. When model's IsSelected set to true for the first time, you may load your data.
XAML:
<TabControl ...>
<TabControl.ItemContainerStyle>
<Style TargetType="{x:Type TabItem}">
<Setter Property="IsSelected"
Value="{Binding Path=IsSelected,Mode=TwoWay}"/>
</Style>
</TabControl.ItemContainerStyle>
</TabControl>
Sample model:
public class MyViewModel : INotifyPropertyChanged
{
private bool _isLoaded;
private void Load()
{
// code
}
private bool _isSelected;
public bool IsSelected
{
get
{
return this._isSelected;
}
set
{
if (this._isSelected != value)
{
this._isSelected = value;
if (this._isSelected && !this._isLoaded)
{
this.Load();
this._isLoaded = true;
}
var propertyChanged = this.PropertyChanged;
if (propertyChanged != null)
{
propertyChanged(this, new PropertyChangedEventArgs("IsSelected"));
}
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…