Click here to Skip to main content
15,914,943 members
Home / Discussions / WPF
   

WPF

 
GeneralRe: Can anyone recommend a free WPF editor? Pin
Slacker00723-Jul-21 5:01
professionalSlacker00723-Jul-21 5:01 
GeneralRe: Can anyone recommend a free WPF editor control? Pin
Richard Deeming27-Jul-21 23:16
mveRichard Deeming27-Jul-21 23:16 
GeneralRe: Can anyone recommend a free WPF editor control? Pin
Mike Hankey28-Jul-21 2:05
mveMike Hankey28-Jul-21 2:05 
QuestionInstalling Test Mode vs Production Mode Pin
Kevin Marois19-Jul-21 8:55
professionalKevin Marois19-Jul-21 8:55 
QuestionRouted Event Args Null Pin
Kevin Marois28-Jun-21 16:28
professionalKevin Marois28-Jun-21 16:28 
AnswerRe: Routed Event Args Null Pin
Richard Deeming28-Jun-21 22:34
mveRichard Deeming28-Jun-21 22:34 
QuestionHow to change background color in combobox in code behind? Pin
Cường Nguyễn Văn 202123-Jun-21 6:56
Cường Nguyễn Văn 202123-Jun-21 6:56 
QuestionMake textbox fill up and resizie Pin
Acuena21-Jun-21 20:59
Acuena21-Jun-21 20:59 
AnswerRe: Make textbox fill up and resizie Pin
Richard Deeming21-Jun-21 21:58
mveRichard Deeming21-Jun-21 21:58 
AnswerRe: Make textbox fill up and resizie Pin
Gerry Schmitz22-Jun-21 7:07
mveGerry Schmitz22-Jun-21 7:07 
Question[Problem] Why can not Pass Image between 2 Views by using the same ViewModel (MVVM) Pin
Oscar Tsai18-Jun-21 7:34
Oscar Tsai18-Jun-21 7:34 
AnswerRe: [Problem] Why can not Pass Image between 2 Views by using the same ViewModel (MVVM) Pin
Gerry Schmitz19-Jun-21 14:29
mveGerry Schmitz19-Jun-21 14:29 
GeneralRe: [Problem] Why can not Pass Image between 2 Views by using the same ViewModel (MVVM) Pin
Oscar Tsai20-Jun-21 3:20
Oscar Tsai20-Jun-21 3:20 
GeneralRe: [Problem] Why can not Pass Image between 2 Views by using the same ViewModel (MVVM) Pin
Gerry Schmitz20-Jun-21 6:01
mveGerry Schmitz20-Jun-21 6:01 
AnswerRe: [Problem] Why can not Pass Image between 2 Views by using the same ViewModel (MVVM) Pin
Richard Deeming20-Jun-21 23:37
mveRichard Deeming20-Jun-21 23:37 
GeneralRe: [Problem] Why can not Pass Image between 2 Views by using the same ViewModel (MVVM) Pin
Oscar Tsai21-Jun-21 4:10
Oscar Tsai21-Jun-21 4:10 
GeneralRe: [Problem] Why can not Pass Image between 2 Views by using the same ViewModel (MVVM) Pin
Oscar Tsai21-Jun-21 16:11
Oscar Tsai21-Jun-21 16:11 
AnswerRe: [Problem] Why can not Pass Image between 2 Views by using the same ViewModel (MVVM) Pin
michaelbarb23-Sep-21 10:32
michaelbarb23-Sep-21 10:32 
GeneralRe: [Problem] Why can not Pass Image between 2 Views by using the same ViewModel (MVVM) Pin
Oscar Tsai13-Oct-21 15:14
Oscar Tsai13-Oct-21 15:14 
GeneralRe: [Problem] Why can not Pass Image between 2 Views by using the same ViewModel (MVVM) Pin
michaelbarb14-Oct-21 12:38
michaelbarb14-Oct-21 12:38 
QuestionProblem Setting Focus Pin
Kevin Marois24-May-21 9:39
professionalKevin Marois24-May-21 9:39 
AnswerRe: Problem Setting Focus Pin
Richard Deeming24-May-21 21:56
mveRichard Deeming24-May-21 21:56 
QuestionImpossible to override a value in an animation located in a style? Pin
Mc_Topaz20-May-21 6:51
Mc_Topaz20-May-21 6:51 
AnswerRe: Impossible to override a value in an animation located in a style? Pin
Gerry Schmitz20-May-21 8:08
mveGerry Schmitz20-May-21 8:08 
QuestionApp Security Behavior Pin
Kevin Marois20-May-21 6:09
professionalKevin Marois20-May-21 6:09 
I'm working on implementing security into my application. When a user logs in a User instance is returned, and on that is a List<string> property of right, which looks like this:
List _userRights = new List
{
    "can_add_employees",
    "can_edit_employees",
};
The tags in the list are stored in a table. If the tag is returned, then the user has access. If it's not returned, then the user does not have acccess. The Security piece is already done.

Now, I want to implement it in XAML. Richard Deeming gave me some direction on this a while back and I've built on his idea. I now have this Behavior class: (thanks Richard)
public static class SecurityBehavior
{
    private static readonly char[] TagSeparators = { '|' };

    #region Allow Enable
    public static readonly DependencyProperty AllowEnableProperty =
        DependencyProperty.RegisterAttached("AllowEnable",
        typeof(bool),
        typeof(SecurityBehavior),
        new PropertyMetadata(false, OnSecurityChanged));

    public static bool GetAllowEnable(DependencyObject obj)
    {
        return (bool)obj.GetValue(AllowEnableProperty);
    }

    public static void SetAllowEnable(DependencyObject obj, bool value)
    {
        obj.SetValue(AllowEnableProperty, value);
    }
    #endregion

    #region Security Tags
    public static readonly DependencyProperty SecurityTagsProperty =
        DependencyProperty.RegisterAttached("SecurityTags",
        typeof(string), typeof(SecurityBehavior),
        new PropertyMetadata("", OnSecurityChanged));

    public static string GetSecurityTags(DependencyObject obj)
    {
        return (string)obj.GetValue(SecurityTagsProperty);
    }

    public static void SetSecurityTags(DependencyObject obj, string value)
    {
        obj.SetValue(SecurityTagsProperty, value);
    }

    private static void OnSecurityChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
    {
        bool canEnable = GetAllowEnable(sender);
        if (canEnable)
        {
            string[] tags = GetSecurityTags(sender).Split(TagSeparators, StringSplitOptions.RemoveEmptyEntries);
            foreach (string tag in tags)
            {
                canEnable &= SecurityService.HasRights(tag);
            }
        }

        var element = (UIElement)sender;
        element.IsEnabled = canEnable;
    }
    #endregion
}
Notice that it calls a static SecurityService class:
public static class SecurityService
{
    private static List<string> _userRights;

    public static bool HasRights(string right)
    {
        return _userRights.Contains(right);
    }

    // This is here for this demo. In production this will be read
    // in from the security rights table
    static SecurityService()
    {
        _userRights = new List
        {
            "can_add_employees",
            "can_edit_employees",
        };
    }
}
I then use it like this in XAML:
<TextBox Grid.Row="0" 
         Grid.Column="1"
         Text="{Binding EmployeeName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
         local:SecurityBehavior.AllowEnable="{Binding AreFieldsEnabled, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
         local:SecurityBehavior.SecurityTags="can_add_employees|can_edit_employees"/>
"AreFieldsEnabled" is a bool property on the base ViewModel that is set to true when the view is in either Add or Edit mode.

This works, there are a couple of issues I'm stuck on.
  1. This approach requires that I have the tags listed in the XAML. I can then add or remove tags to any specific UI element as needed, but it requires going back into the XAML to do so. I was considering setting the element's Name and have the service look it up and determine if it can be enabled.
  2. In some cases there are addition factors that determine if/when a UI element should be enabled, such as another checkbox beging checked, or a specific combox box item being selected somewhere else.
I'd like to hear suggestions.

Thanks
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.