views:

165

answers:

1

Hi I found this questions

http://stackoverflow.com/questions/1019204/how-to-manipulate-wpf-gui-based-on-user-roles/1020303#1020303

and apply his answer to my project. I'm implementing the same behavior.

but the thing is the property binding doesn't work.

I create the RoleToVisibilityConverter class and make some test, calling the convert

RoleToVisibilityConverter conv = new RoleToVisibilityConverter();  
conv.Convert((object)Thread.CurrentPrincipal,cbo_organismo.GetType(),(object)"editor",System.Globalization.CultureInfo.CurrentCulture);

this work find, then i add the resources to my usercontrol

<UserControl.Resources>
<fundafe:RoleToVisibilityConverter x:Key="roleConverter"/>
</UserControl.Resources>

and make the binding to my button control

<Button Margin="0,0,0,0" Visibility="{Binding Source=Thread.CurrentPrincipal,  Path=CurrentPrincipal, Converter={StaticResource roleConverter}, ConverterParameter=editor}" VerticalAlignment="Center" HorizontalAlignment="Left"  Name="btn_Eliminar" Click="btn_Eliminar_Click" Width="Auto" Height="25" Background="Transparent" BorderBrush="Transparent">
                         <Image Name="img_eliminar" Width="48" Source="imagenes/borrar.png" Height="19" />
                    </Button>

after running my application, the button is still visible.

if i'm hardcoding the Visibility property the button is hiden

btn_Eliminar.Visibility=(Visibility)conv.Convert((object)Thread.CurrentPrincipal,cbo_organismo.GetType(),(object)"editor",System.Globalization.CultureInfo.CurrentCulture)

Any suggestion?

Note: I'cant use the last approach because in my real scenario the button is part of a DataTemplate for a ListView, and even if a capture the button using the TreeHelper the ListView only apply the change to the first items due to virtualization

A: 

Assuming this is your code verbatim, it looks like the Binding on your Button is wrong. You have this:

{Binding Source=Thread.CurrentPrincipal,  Path=CurrentPrincipal, Converter={StaticResource roleConverter}, ConverterParameter=editor}

Notice that you've bound to Thread.CurrentPrincipal as your source, but then you set the Path to CurrentPrincipal as well. This equates to Thread.CurrentPrincipal.CurrentPrincipal which would obviously fail to bind. Honestly I'm pretty sure you need an {x:Static} around the Thread.CurrentPrincipal as well. Plus that means you need a namespace for System.Threading declared. Assuming you define the namespace as "systhreading" so the final binding would be:

{Binding Source={x:Static systhreading:Thread.CurrentPrincipal},  Converter={StaticResource roleConverter}, ConverterParameter=editor}
Drew Marsh
Hi I'm new with wpf so I don't get all this binding thing, thanks for the help.I add this references xmlns:systhreading="clr-namespace:System.Threading;assembly=System" but I get a error: the type Thread is not find, any idea what I'm doing wrong
germandb
The Thread class is actually in the mscorlib assembly. So your namespace declaration should be: xmlns:systhreading="clr-namespace:System.Threading;assembly=mscorlib".
Drew Marsh
thats make the deal, thanks ;)
germandb