Click here to Skip to main content
14,423,188 members

Silverlight / WPF

 
PinnedHOW TO ANSWER A QUESTION PinPopular
Chris Maunder16-Jul-09 4:09
cofounderChris Maunder16-Jul-09 4:09 
PinnedHow to get an answer to your question PinPopular
Chris Maunder16-Jul-09 4:05
cofounderChris Maunder16-Jul-09 4:05 
QuestionIm new bie help me fix 1 erorr on wpf, i cant do it? Pin
Member 1468037212-Jan-20 17:07
MemberMember 1468037212-Jan-20 17:07 
GeneralRe: Im new bie help me fix 1 erorr on wpf, i cant do it? Pin
Richard MacCutchan12-Jan-20 23:12
communityengineerRichard MacCutchan12-Jan-20 23:12 
AnswerRe: Im new bie help me fix 1 erorr on wpf, i cant do it? Pin
ZurdoDev13-Jan-20 2:07
mveZurdoDev13-Jan-20 2:07 
QuestionRoutedEvent Pass EventArg Pin
Kevin Marois11-Jan-20 17:42
professionalKevin Marois11-Jan-20 17:42 
AnswerRe: RoutedEvent Pass EventArg Pin
Gerry Schmitz11-Jan-20 21:24
mveGerry Schmitz11-Jan-20 21:24 
GeneralRe: RoutedEvent Pass EventArg Pin
Kevin Marois12-Jan-20 11:05
professionalKevin Marois12-Jan-20 11:05 
GeneralRe: RoutedEvent Pass EventArg Pin
Gerry Schmitz13-Jan-20 14:23
mveGerry Schmitz13-Jan-20 14:23 
GeneralRe: RoutedEvent Pass EventArg Pin
Kevin Marois14-Jan-20 6:18
professionalKevin Marois14-Jan-20 6:18 
GeneralRe: RoutedEvent Pass EventArg Pin
Kevin Marois14-Jan-20 6:43
professionalKevin Marois14-Jan-20 6:43 
AnswerRe: RoutedEvent Pass EventArg Pin
Richard Deeming13-Jan-20 9:00
communityengineerRichard Deeming13-Jan-20 9:00 
GeneralRe: RoutedEvent Pass EventArg Pin
Kevin Marois14-Jan-20 10:02
professionalKevin Marois14-Jan-20 10:02 
QuestionCalculate the textbox value based on the value of combobox ? ( wpf ) Pin
Member 146803727-Jan-20 20:44
MemberMember 146803727-Jan-20 20:44 
QuestionRe: Calculate the textbox value based on the value of combobox ? ( wpf ) Pin
Richard MacCutchan7-Jan-20 22:17
communityengineerRichard MacCutchan7-Jan-20 22:17 
AnswerRe: Calculate the textbox value based on the value of combobox ? ( wpf ) Pin
Member 146803727-Jan-20 22:49
MemberMember 146803727-Jan-20 22:49 
SuggestionRe: Calculate the textbox value based on the value of combobox ? ( wpf ) Pin
Richard Deeming8-Jan-20 1:46
communityengineerRichard Deeming8-Jan-20 1:46 
QuestionRefresh Attached Property? Pin
Kevin Marois7-Jan-20 11:17
professionalKevin Marois7-Jan-20 11:17 
AnswerRe: Refresh Attached Property? Pin
Richard Deeming8-Jan-20 2:18
communityengineerRichard Deeming8-Jan-20 2: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?
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 8:41
professionalKevin Marois30-Dec-19 8:41 
AnswerRe: WPF Exception - Send Error Report Pin
Richard MacCutchan30-Dec-19 11:13
communityengineerRichard MacCutchan30-Dec-19 11:13 
AnswerRe: WPF Exception - Send Error Report Pin
jimmson2-Jan-20 4:15
Memberjimmson2-Jan-20 4:15 
AnswerRe: WPF Exception - Send Error Report Pin
Gerry Schmitz2-Jan-20 21:09
mveGerry Schmitz2-Jan-20 21:09 
AnswerRe: WPF Exception - Send Error Report Pin
Mycroft Holmes3-Jan-20 11:49
MemberMycroft Holmes3-Jan-20 11:49 
QuestionMenuButton CanExecute Firing Wrong Pin
Kevin Marois24-Dec-19 12:42
professionalKevin Marois24-Dec-19 12:42 

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.