Monday, February 5, 2018

How to update a UI Element based on Attached Property?

So here's the scoop, first time working with Attached Properties/Dependency Properties in an MVVM fashion; please be gentle.

The functionality I am looking for is to mimick some sort of visibility control based on a user right using said Attached Properties. Currently, I have the following in a control that is laid out in a user control:

 con:RightControl.CanUserEditForRight="5" 

The RightControl class has:

 public static readonly DependencyProperty CanUserEditForRight = DependencyProperty.RegisterAttached("CanUserEditForRight", typeof(string), typeof(RightControl), new FrameworkPropertyMetadata( false, new PropertyChangedCallback(EditRightsChanged))); private static void EditRightsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { if (d is UIElement && e != null) { string rightToCheck = (string)e.NewValue; if (_service.GetCurrentUser.Count(a => a.RightId == rightToCheck) == 0) (d as UIElement).Visibility = Visibility.Collapsed; else (d as UIElement).Visibility = Visibility.Visible; } } public static void SetCanUserEditForRight(DependencyObject obj, string value) { obj.SetValue(CanUserEditForRight, value); } public static string GetCanUserEditForRight(DependencyObject obj) { return (string)obj.GetValue(CanUserEditForRight); } 

Now this only works if I pass in something that changes via INotifyPropertyChanged.

I need it to be the other way around so when the current user changes, it runs against the attachedproperty.

How to update a UI Element based on Attached Property? Click here
  • Blogger Comment
  • Facebook Comment

0 comments:

Post a Comment

The webdev Team