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:RoleToVisibilityConvert开发者_C百科er 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 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}
精彩评论