Click here to Skip to main content
15,887,453 members
Home / Discussions / WPF
   

WPF

 
AnswerRe: RoutedEvent Pass EventArg Pin
Richard Deeming13-Jan-20 8:00
mveRichard Deeming13-Jan-20 8:00 
GeneralRe: RoutedEvent Pass EventArg Pin
Kevin Marois14-Jan-20 9:02
professionalKevin Marois14-Jan-20 9:02 
QuestionCalculate the textbox value based on the value of combobox ? ( wpf ) Pin
Member 146803727-Jan-20 19:44
Member 146803727-Jan-20 19:44 
QuestionRe: Calculate the textbox value based on the value of combobox ? ( wpf ) Pin
Richard MacCutchan7-Jan-20 21:17
mveRichard MacCutchan7-Jan-20 21:17 
AnswerRe: Calculate the textbox value based on the value of combobox ? ( wpf ) Pin
Member 146803727-Jan-20 21:49
Member 146803727-Jan-20 21:49 
SuggestionRe: Calculate the textbox value based on the value of combobox ? ( wpf ) Pin
Richard Deeming8-Jan-20 0:46
mveRichard Deeming8-Jan-20 0:46 
QuestionRefresh Attached Property? Pin
Kevin Marois7-Jan-20 10:17
professionalKevin Marois7-Jan-20 10:17 
AnswerRe: Refresh Attached Property? Pin
Richard Deeming8-Jan-20 1:18
mveRichard Deeming8-Jan-20 1:18 
As far as I can see, there isn't a way to get a list of elements with a particular attached property set. I suspect you'll need to maintain your own list, using the WeakReference<T> class[^] to avoid a memory leak.

Perhaps something like this?
C#
public static class UIElementEnablerExtension
{
    private static readonly ReaderWriterLockSlim Locker = new ReaderWriterLockSlim();
    private static readonly List<WeakReference<UIElement>> TrackedElements = new List<WeakReference<UIElement>>();
    
    public static readonly DependencyProperty SecurityKeyProperty = DependencyProperty.RegisterAttached(
        "SecurityKey", typeof(string), typeof(UIElementEnablerExtension),
        new UIPropertyMetadata("", OnSecurityKeyPropertyChanged));
    
    public static string GetSecurityKey(DependencyObject obj) => (string)obj.GetValue(SecurityKeyProperty);
    
    public static void SetSecurityKey(DependencyObject obj, string value) => obj.SetValue(SecurityKeyProperty);
    
    private static void OnSecurityKeyPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        if (d is UIElement element)
        {
            Locker.EnterWriteLock();
            try
            {
                TrackedElements.Add(new WeakReference<UIElement>(element));
            }
            finally
            {
                Locker.ExitWriteLock();
            }
            
            EnableOrDisableElement(element);
        }
    }
    
    public static void OnCurrentUserChanged()
    {
        // Process a copy of the list, to avoid holding the lock for too long:
        List<WeakReference<UIElement>> elements;
        Locker.EntryReadLock();
        try
        {
            elements = new List<WeakReference<UIElement>>(TrackedElements.Count);
            elements.AddRange(TrackedElements);
        }
        finally
        {
            Locker.ExitReadLock();
        }

        var elementsToRemove = new List<WeakReference<UIElement>>();
        foreach (WeakReference<UIElement> item in elements)
        {
            if (item.TryGetTarget(out UIElement element))
            {
                EnableOrDisableElement(element);
            }
            else
            {
                elementsToRemove.Add(item);
            }
        }
        
        if (elementsToRemove.Count != 0)
        {
            // Remove any dead elements from the list:
            Locker.EnterWriteLock();
            try
            {
                foreach (WeakReference<UIElement> item in elementsToRemove)
                {
                    TrackedElements.Remove(item);
                }
            }
            finally
            {
                Locker.ExitWriteLock();
            }
        }
    }
    
    private static void EnableOrDisableElement(UIElement element)
    {
        string accessKey = GetSecurityKey(element);
        if (string.IsNullOrEmpty(accessKey))
        {
            element.IsEnabled = true;
            return;
        }
        
        bool hasAccess = AppCore.CurrentUser?.Rights.Any(x => string.Equals(x, accessKey, StringComparison.Ordinal)) ?? false;
        element.IsEnabled = hasAccess;
    }
}
You'll need to call UIElementEnablerExtension.OnCurrentUserChanged when the user logs in, either directly or using a mediator.



"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer

QuestionWPF Exception - Send Error Report Pin
Kevin Marois30-Dec-19 7:41
professionalKevin Marois30-Dec-19 7:41 
AnswerRe: WPF Exception - Send Error Report Pin
Richard MacCutchan30-Dec-19 10:13
mveRichard MacCutchan30-Dec-19 10:13 
AnswerRe: WPF Exception - Send Error Report Pin
jimmson2-Jan-20 3:15
jimmson2-Jan-20 3:15 
AnswerRe: WPF Exception - Send Error Report Pin
Gerry Schmitz2-Jan-20 20:09
mveGerry Schmitz2-Jan-20 20:09 
AnswerRe: WPF Exception - Send Error Report Pin
Mycroft Holmes3-Jan-20 10:49
professionalMycroft Holmes3-Jan-20 10:49 
QuestionMenuButton CanExecute Firing Wrong Pin
Kevin Marois24-Dec-19 11:42
professionalKevin Marois24-Dec-19 11:42 
AnswerRe: MenuButton CanExecute Firing Wrong Pin
Gerry Schmitz26-Dec-19 6:34
mveGerry Schmitz26-Dec-19 6:34 
GeneralRe: MenuButton CanExecute Firing Wrong Pin
Kevin Marois26-Dec-19 7:38
professionalKevin Marois26-Dec-19 7:38 
GeneralRe: MenuButton CanExecute Firing Wrong Pin
Gerry Schmitz27-Dec-19 5:58
mveGerry Schmitz27-Dec-19 5:58 
AnswerRe: MenuButton CanExecute Firing Wrong Pin
Mycroft Holmes27-Dec-19 14:21
professionalMycroft Holmes27-Dec-19 14:21 
GeneralRe: MenuButton CanExecute Firing Wrong Pin
Kevin Marois27-Dec-19 14:34
professionalKevin Marois27-Dec-19 14:34 
GeneralRe: MenuButton CanExecute Firing Wrong Pin
Mycroft Holmes27-Dec-19 14:48
professionalMycroft Holmes27-Dec-19 14:48 
GeneralRe: MenuButton CanExecute Firing Wrong Pin
Kevin Marois27-Dec-19 14:53
professionalKevin Marois27-Dec-19 14:53 
GeneralRe: MenuButton CanExecute Firing Wrong Pin
Mycroft Holmes27-Dec-19 15:20
professionalMycroft Holmes27-Dec-19 15:20 
AnswerRe: MenuButton CanExecute Firing Wrong Pin
Richard Deeming7-Jan-20 8:53
mveRichard Deeming7-Jan-20 8:53 
QuestionWPF Pin
MrsealIsHotChicken18-Dec-19 14:41
MrsealIsHotChicken18-Dec-19 14:41 
AnswerRe: WPF Pin
Gerry Schmitz19-Dec-19 7:11
mveGerry Schmitz19-Dec-19 7:11 

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.