Reputation: 2257
I have a user control displayed as many instances under tab controls
<TabControl x:Name="tabControl" HorizontalAlignment="Left" Height="616" Margin="0,20,0,20" VerticalAlignment="Top" Width="1282" Grid.ColumnSpan="2">
<TabItem Header=" OPHistorianCutter " Padding="0">
<OP:Historian x:Name="OPHistorianCutter1" />
</TabItem>
<TabItem Header=" OPHistorianCutter " Padding="0">
<OP:Historian x:Name="OPHistorianCutter2" />
</TabItem>
<TabItem Header=" OPHistorianCutter " Padding="0">
<OP:Historian x:Name="OPHistorianCutter3" />
</TabItem>
</TabControl>
My question:
Is there any event I can use in the 'OPHistorian' User Control which executes when a Usercontrol is tabbed for display? I tried 'GotFocus' but doesn't seem to work.
Is there any Property which tells me that the User Control is currently under display (been selected under tab control). I tried 'OPHistorian1.isFocused' but seems like it's always false
Upvotes: 0
Views: 2341
Reputation: 6538
1 . You can use the SelectionChanged
event of the tabControl to know which tabItem is displayed.
SelectionChanged="TabControl_OnSelectionChanged"
private void TabControl_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
throw new NotImplementedException();
}
2 . You can use SelectedIndex
to get the selected tab :
tabControl.SelectedIndex
or with the tabItem
tabItem.IsSelected
You can report these properties on the the UserControl by getting the property from the parent control:
public bool IsSelected
{
get
{
var tabitem = Parent as TabItem;
return tabitem != null && tabitem.IsSelected;
}
}
EDIT
You can play with the isVisible Property and the associated event :
<TabControl>
<TabItem Header="1">
</TabItem>
<TabItem Header="2">
<local:UserControl1 IsVisibleChanged="UIElement_OnIsVisibleChanged"></local:UserControl1>
</TabItem>
<TabItem></TabItem>
</TabControl>
private void UIElement_OnIsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
{
var uc = sender as UserControl1;
}
Upvotes: 1
Reputation: 2257
Thank you for all the responses, but I've found a solution. Have to use visualtreehelper to find the parent container, and see if it is within visual bounds.
public bool IsUserVisible
{
get
{
UIElement element = (UIElement)this;
if (!element.IsVisible)
return false;
var container = VisualTreeHelper.GetParent(element) as FrameworkElement;
if (container == null) throw new ArgumentNullException("container");
Rect bounds = element.TransformToAncestor(container).TransformBounds(new Rect(0.0, 0.0, element.RenderSize.Width, element.RenderSize.Height));
Rect rect = new Rect(0.0, 0.0, container.ActualWidth, container.ActualHeight);
return rect.IntersectsWith(bounds);
}
}
Upvotes: 0
Reputation: 102
1 . You can use the SelectionChanged event of the tabControl to know which tabItem (and UserControl) is displayed.
Upvotes: 1