Noir
Noir

Reputation: 13

Binding XAML Visibility attribute to Thread.CurrentPrincipal

In my WPF client Application I use GenericIdentity for security:

GenericIdentity MyIdentity = new GenericIdentity("Identity");
String[] MyStringArray = { "Admin", "Editor" };
GenericPrincipal MyPrincipal = new GenericPrincipal(MyIdentity, MyStringArray);

Now I want to show/hide WPF objects according to user role. Iv'e tried several methods such as:

Visibility="{Binding Source=Thread.CurrentPrincipal, Converter={StaticResource RoleToVisibilityConverter}, ConverterParameter=Administrator}"

which make the 'value' object in the converter a String of "Thread.CurrentPrincipal", also tried:

Visibility="{Binding Path=Thread.CurrentPrincipal, Converter={StaticResource RoleToVisibilityConverter}, ConverterParameter=Administrator}" 

And

Visibility="{Binding Source=Thread.CurrentPrincipal, Path=CurrentPrincipal, Converter={StaticResource RoleToVisibilityConverter}, ConverterParameter=Administrator}"

which skip the converter entirely. This is my first C#/.net program so I don't really have a lot of knowledge in the area, Would greatly appreciate a solution. thanks!

Upvotes: 1

Views: 585

Answers (2)

Louis Kottmann
Louis Kottmann

Reputation: 16648

In code-behind (C#), you need to set the DataContext of your control to the object that contains Thread.CurrentPrincipal. Then in XAML, you do it the second way.

Upvotes: 0

svick
svick

Reputation: 244948

First, to access static properties, you need to use the x:Static markup extension:

Visibility="{Binding Source={x:Static Threading:Thread.CurrentPrincipal},
    Converter={StaticResource RoleToVisibilityConverter}, ConverterParameter=Admin}"

This assumes you have

xmlns:Threading="clr-namespace:System.Threading;assembly=mscorlib"

on the root element of your XAML.

Second, you don't show how you set the principal, but you have to do it using AppDomain.SetThreadPrincipal().

Third, you set the role to Admin, but then check for Administrator.

Upvotes: 2

Related Questions