Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

ObjectDataProvider ObjectInstance bind to static object

$
0
0

 

Hi, I'm having a hard time figuring out how to use a static object as the ObjectInstance for an ObjectDataProvider.  Basically, I have a static class User that handles user information (permissions, login name, etc) and is initialized in the OnStartup() method of my WPF app.  This User class has a static method, HasPermission(UserPermission perm), where UserPermission is an enum.  I want to set the enabled/disabled state on various UI elements based on permissions.  So what I have now is similar to the following:

 

public enum UserPermission{

SomePermission,

OtherPermission

}

 

public static class User {

public static Initialize() {

// Called via OnStartup, sets initial values prior to any UI being enabled

}

 

public static bool HasPermission(PermissionType perm) {

// Return true/false

}

}

 

Now inside the XAML, I've been trying to do the following:

 

<

ObjectDataProvider x:Key="hasPermission" ObjectInstance="{x:Static Member=local:User}" MethodName="HasPermission">

<!--ObjectDataProvider x:Key="hasPermission" ObjectInstance="{x:Type local:User}" MethodName="HasPermission"-->

<ObjectDataProvider.MethodParameters>

<!--x:Type TypeName="frg:UserPermission"/-->

<local:UserPermission>SomePermission</local:UserPermission>

</ObjectDataProvider.MethodParameters>

</ObjectDataProvider>

 

The current approach will fail saying ""local:User" member is not valid because it does not have a qualifying type".  The commented ObjectDataProvider line will fail with the following message:

 

System.Windows.Data Error: 30 : ObjectDataProvider: Failure trying to invoke method on type; Method='HasPermission'; Type='RuntimeType'; Error='No method was found with matching parameter signature.' MissingMethodException:'System.MissingMethodException: Method 'System.RuntimeType.HasPermission' not found.

 

I have also tried the following approach:

<ObjectDataProvider ObjectType="{x:Type local:User}" MethodName="HasPermission" x:Key="hasPermission">

which will also fail with the message "Object reference not set to an instance of an object" at compile time.

 

What I'm trying to accomplish, ideally, is to be able to bind the IsVisible attribute of some UI elements to the HasPermission method and be able to pass in the permission type at the point of binding, but I will also settle for having to setup an ObjectDataProvider and hardcode the permission type at that point, as is done above.  What am I doing wrong and how do I work around this issue?  Thanks!

 

 


Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>