user1034912
user1034912

Reputation: 2257

How to know a WPF User Control is selected and under display?

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:

  1. 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.

  2. 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

Answers (3)

Quentin Roger
Quentin Roger

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

user1034912
user1034912

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

Juan J. S&#225;ez
Juan J. S&#225;ez

Reputation: 102

1 . You can use the SelectionChanged event of the tabControl to know which tabItem (and UserControl) is displayed.

  1. You can use the Loaded event of the UserControl to know when it's activated. Read this.

Upvotes: 1

Related Questions