Hi I found this questions
How to manipulate WPF GUI based on user roles
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 hidden
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
Assuming this is your code verbatim, it looks like the Binding on your
Buttonis wrong. You have this:Notice that you’ve bound to
Thread.CurrentPrincipalas your source, but then you set thePathtoCurrentPrincipalas well. This equates toThread.CurrentPrincipal.CurrentPrincipalwhich would obviously fail to bind.Honestly I’m pretty sure you need an
{x:Static}around theThread.CurrentPrincipalas well. Plus that means you need a namespace forSystem.Threadingdeclared. Assuming you define the namespace as “systhreading” so the final binding would be: