L3n95
L3n95

Reputation: 1615

Access Window property from UserControl child

I have a MainWindow with a TabControl. Each Tab is a UserControl existing in a different file.

...
<TabControl>
   <TabItem>
      <local:Tab1>
   </TabItem>
...
   <TabItem>
      <local:Tab2>
   </TabItem>
</TabControl>

These UserControls should act different depending on the access rights. The access rights (an int) are passed to the Main Window after the login screen via:

MainWindow mainWindow = new MainWindow(accessRights);
mainWindow.show();

Now I have the access rights in the MainWindow.xaml.cs. But how can I access these access rights in the UserControls.

Upvotes: 1

Views: 3383

Answers (4)

verm-luh
verm-luh

Reputation: 184

Pass a reference of your MainWindow instance to the constructor of your UserControl instances while creating them. By doing this you gain access to public properties of your containing main form in the UserControls code.

Upvotes: 0

ivica.moke
ivica.moke

Reputation: 1064

Try with EventAggregator from Prism.Core, Basicly u set Publish method where you pass your int and in that user control Subscribe to that event, event will be in your case Window loaded.

MainWindowViewModel

   public MainWindowViewModel( IEventAggregator eventAggregator)
    {
        eventAggregator.GetEvent<PassingIntToUserControlEvent>().Publish(HereGoesYourInt);
    }

UserControlViewModel

 public UserControlViewModel( IEventAggregator eventAggregator)
    {
        eventAggregator.GetEvent<PassingIntToUserControlEvent>().Subscribe(SomeMethodYouWantWithYourInt);
    }

and to set that PassingIntToUserControlEvent is so easy.

public class PassingIntToUserControlEvent : PubSubEvent<int>{}

and your set to go.

This video has basic introdction to Prism and some of it's component, one of them is EventAggregator. I've found it to be very usefull. Hope it helps. https://www.youtube.com/watch?v=ZfBy2nfykqY

Upvotes: 0

mm8
mm8

Reputation: 169390

You could add a dependency property to each UserControl class:

public class Tab1 : UserControl
{
    ...

    public Boolean HasAccess
    {
        get { return (Boolean)this.GetValue(HasAccessProperty); }
        set { this.SetValue(HasAccessProperty, value); }
    }
    public static readonly DependencyProperty HasAccessProperty = DependencyProperty.Register(
      "HasAccess", typeof(Boolean), typeof(Tab1), new PropertyMetadata(false));
}

...and bind it to a public property of the parent window in your XAML markup:

<TabControl>
    <TabItem>
        <local:Tab1 HasAccess="{Binding Path=WindowProperty, RelativeSource={RelativeSource AncestorType=Window}}" />
    </TabItem>
    ...
</TabControl>

How to: Implement a Dependency Property: https://msdn.microsoft.com/en-us/library/ms750428(v=vs.110).aspx

Make sure that the window class exposes the access right(s) using a public property because you cannot bind to fields.

The other option would be to get a reference to the parent window using the Window.GetWindow method in the code-behind of the UserControl once it has been loaded:

public partial class MyUserControl : UserControl
{
    public MyUserControl()
    {
        InitializeComponent();
        Loaded += (s, e) => 
        {
            MainWindow parentWindow = Window.GetWindow(this) as MainWindow;
            if(parentWindow != null)
            {
                //access property of the MainWindow class that exposes the access rights...
            }
        };
    }
}

Upvotes: 4

Rahul M
Rahul M

Reputation: 91

Add this logic to your User-Controls:

MainWindow mw = (MainWindow)this.Parent;
accessRights = mw.accessRights;

This is the logic and you may need to change above code to match syntax etc.

Upvotes: 1

Related Questions