I've seen a library that allows me to do this inside my XAML, which sets the visibility of the control based on whether or not the user is in a role:
s:Authorization.RequiresRole="Admin"
Using that library with my database requires a bunch of coding that I can't really do right now. Ultimately here's what I want to know...
I have received the authenticated users role from my SPROC, and its currently stored in my App.xaml.cs as a property (not necessary for the final solution, just FYI for now). I want to create a property (dependency property? attached property?) that allows me to say something very similar to what the other library has: RequiresRole="Admin", which would collapse the visibility if the user is not in the Admin role. Can anyone point me in the right direction on this?
EDIT
After building the authorization class, I get the following error:
"The property 'RequiredRole' does not exist on the type 'HyperlinkButton' in the XML Namespace clr-namespace:TSMVVM.Authorization"
I'm trying to add this xaml:
<HyperlinkButton x:Name="lnkSiteParameterDefinitions"
Style="{StaticResource LinkStyle}"
Tag="SiteParameterDefinitions"
Content="Site Parameter Definitions"
Command="{Binding NavigateCommand}"
s:Authorization.RequiredRole="Admin"
CommandParameter="{Binding Tag, ElementName=lnkSiteParameterDefinitions}"/>
When I started typing the s:Authorization.RequiredRole="Admin", intellisense picked it up. I tried setting the typeof(string) and typeof(ownerclass) to HyperlinkButton to see if that would help, but it didn't. Any thoughts?
Attached property is the way to implement it. You should define a property like this:
public class Authorization
{
#region Attached DP registration
public static string GetRequiredRole(UIElement obj)
{
return (string)obj.GetValue(RequiredRoleProperty);
}
public static void SetRequiredRole(UIElement obj, string value)
{
obj.SetValue(RequiredRoleProperty, value);
}
#endregion
// Using a DependencyProperty as the backing store for RequiredRole. This enables animation, styling, binding, etc...
public static readonly DependencyProperty RequiredRoleProperty =
DependencyProperty.RegisterAttached("RequiredRole", typeof(string), typeof(Authorization), new PropertyMetadata(RequiredRole_Callback));
// This callback will be invoked when some control will receive a value for your 'RequiredRole' property
private static void RequiredRole_Callback(DependencyObject source, DependencyPropertyChangedEventArgs e)
{
var uiElement = (UIElement) source;
RecalculateControlVisibility(uiElement);
// also this class should subscribe somehow to role changes and update all control's visibility after role being changed
}
private static void RecalculateControlVisibility(UIElement control)
{
//Authorization.UserHasRole() - is your code to check roles
if (Authentication.UserHasRole(GetRequiredRole(control)))
control.Visibility = Visibility.Visible;
else
control.Visibility = Visibility.Collapsed;
}
}
PS: Have noticed too late that you were asking about Silverlight. Though I believe it works in the same way there, but I've tried it only on WPF.
Related
I need to add some logic to user control with DependencyProperty.
My logic is supposed to change properties on controls inside my UserControl.
I want to avoid building huge "dependency tree" because I have a lot of user controls. I just want to use binding in my windows (not in nested user controls).
This is my control:
public partial class BucketElevatorControl : UserControl
{
public BucketElevatorControl()
{
InitializeComponent();
}
public bool On
{
get
{
return (bool)GetValue(OnProperty);
}
set
{
SetValue(OnProperty, value);
}
}
// Using a DependencyProperty as the backing store for IsOn. This enables animation, styling, binding, etc...
public static readonly DependencyProperty OnProperty = DependencyProperty.Register(
"On",
typeof(bool),
typeof(BucketElevatorControl),
new PropertyMetadata(
false, PropertyChangedCallback
));
private static void PropertyChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
{
// I want to do something with my UserControl child controls
}
}
The question is: how can I do some logic in contol code behind and take advantage of data binding?
My logic is complicated (drawing graphics, animations etc.).
You should create CoerceValueCallbacks for the properties you want to change. Those callbacks set the new values. When this property changes, you then coerce the others, like so:
private static void PropertyChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
{
dependencyObject.CoerceValue(MinReadingProperty);
dependencyObject.CoerceValue(MaxReadingProperty);
}
I have no idea what you mean by "dependency tree", but if you want to alter the state of stuff in your template according to changes in your control's dependency properties, you can do that with TemplateBinding and/or triggers in your control template. Write value converters if you need to. Most of what you need to do can probably be done that way.
If you need more complicated logic, you can also override OnApplyTemplate() in your control, and call GetTemplateChild() to get named controls within the control's template. For example, you might require the template to have a TextBox somewhere in it called PART_FooText; throw an exception if you get null from GetTemplateChild("PART_FooText") as TextBox. If the TextBox is there, do anything you like to it: Handle events, set properties, etc. If you like, keep a private field TextBox _PART_FooText; to fiddle with it later on, in your property-changed callbacks, other events, or whatever.
I'm very new to WPF and while studying (particularly creating a user control), I stumbled upon this thing called "DependencyProperty".
I understand how it works in code but why and when do we need it when I can just create a property and expose it for public use.
Example:
XAML:
<UserControl.....>
<StackPanel Orientation="Vertical">
<TextBlock x:Name="label" Text="Hello"/>
<TextBlock x:Name="text" Text="World!" />
</StackPanel>
</UserControl>
CS file:
public partial SampleUserCtrl : UserControl
{
public string LabelText { get { return this.label.Text; } set { this.label.Text = value; } }
public string TextBoxText { get { return this.text.Text; } set { this.text.Text = value; } }
}
DependecyProperty in WPF has different uses.
Advantages compared to normal .NET property
Reduced memory footprint It's a huge dissipation to store a field for
each property when you think that over 90% of the properties of a UI
control typically stay at its initial values. Dependency properties
solve these problems by only store modified properties in the
instance. The default values are stored once within the dependency
property.
Value inheritance When you access a dependency property the value is
resolved by using a value resolution strategy. If no local value is
set, the dependency property navigates up the logical tree until it
finds a value. When you set the FontSize on the root element it
applies to all textblocks below except you override the value.
Change notification Dependency properties have a built-in change
notification mechanism. By registering a callback in the property
metadata you get notified, when the value of the property has been
changed. This is also used by the databinding.
As you dig deeper to WPF you will also stumble upon DataBinding and object-oriented design patterns like MVVM or MVPVM. Both patterns rely on DataBinding which is achieved through using of Dependency Properties. You cannot perform data binding if it is not a dependency property.
Basically data binding through dependency properties allow the user to update the view when updating a value through code.
Ex: In Windows Forms in order to update a label text you assign its value on the code behind like this:
lbl1.Text = "foo";
In data binding where as you bind in the XAML (View)
<label Text = "{Binding foo}"></label>
and update the values in your code behind:
foo = "foo";
I am not an expert myself so sorry if I might sound confusing.
Dependency property has many benefits over normal property.
A dependency property value can be set by referencing a resource
It can reference a value through data binding
It can be animated. When an animation is
applied and is running, the animated value operates at a higher
precedence than any value (such as a local value) that the property
otherwise has.
You can learn more about it from msdn.
The main advantage of DP property you can find anything in DP like your Control info and you current DataContext info.
public string MyProperty
{
get { return (string)GetValue(MyPropertyProperty); }
set { SetValue(MyPropertyProperty, value); }
}
// Using a DependencyProperty as the backing store for MyProperty. This enables animation, styling, binding, etc...
public static readonly DependencyProperty MyPropertyProperty =
DependencyProperty.Register("MyProperty", typeof(string), typeof(MyUserControl), new PropertyMetadata(null, (s, e) => OnChangedValue(s, e)));
private static void OnChangedValue(DependencyObject s, DependencyPropertyChangedEventArgs e)
{
throw new NotImplementedException();
}
Windows Phone 7.1 project (WP 8.0 SDK), I want to pass current item in ItemTemplate to a user control.
XAML:
<ItemsControl.ItemTemplate>
<DataTemplate>
<local:ShipControl Ship="{Binding}" x:Name="ShipControl"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
Code behind ShipControl:
public object Ship
{
get
{
return GetValue(ShipProperty);
}
set
{
SetValue(ShipProperty, value);
}
}
//Used by xaml binding
public static readonly DependencyProperty ShipProperty = DependencyProperty.Register("Ship", typeof(Ship), typeof(Ship), new PropertyMetadata(null, new PropertyChangedCallback(OnShipChanged)));
private static void OnShipChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
//TODO: Set break point here
return;
}
However, when debugging Ship an object of value DataBinding is passed as value, not a Ship (therefore return type is object instead of Ship). That eventually causes an exception on SetValue.
Other bindings on Ship-properties do work, so I really have no idea. According to this question, above should work:
WPF Pass current item from list to usercontrol
See here for sample project which throws exception on data binding, because passed object is Binding instead of data object. http://dl.dropbox.com/u/33603251/TestBindingApp.zip
You need to put a x:Name="MyControl" in your control, and then your binding will look like Ship="{Binding ElementName=MyList, Path=CurrentItem}" instead of just {Binding} (which does not mean much AFAIK). Your control needs to expose the CurrentItem property.
If you do not want to explicity name your control, you can try to play with Relative Source but I did not try myself so cannot help you on this one.
Your Dependency Property is badly formed so the XAML parser does not treat it as such.
You need to change your instance property type to Ship, and DependencyProperty owner type to ShipControl. Then the Binding will work (assuming that you are binding to a list of Ships).
public Ship Ship
{
get { return (Ship)GetValue(ShipProperty); }
set { SetValue(ShipProperty, value); }
}
public static readonly DependencyProperty ShipProperty =
DependencyProperty.Register("Ship", typeof(Ship), typeof(ShipControl), new PropertyMetadata(null, new PropertyChangedCallback(OnShipChanged)));
private static void OnShipChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
//TODO: Set break point here
return;
}
Short story
I have a BindingExpression object, I want to update the target side of the binding just like I can do it with the source (by calling BindingExpression.UpdateSource). How can I do it?
There is the similar question for WPF which I don't know how to adapt to Silverlight: Cancel combobox selection in WPF with MVVM
Long story
Given a two-way binding that binds the SelectedItem of a combobox to a property of a view model. The combobox is used for navigation so that by selecting the user tells the view model to navigate away. The view model can have some unsaved changes which the user don't want to loose accidently. So the view model throws out a confirmation dialog asking if the user really wants to navigate away and loose their changes. In case the user says no, we want to undo the selection made to the combobox, so that it was in it's original state just like before the navigation attempt was made. How can I do it?
For WPF people
Please note, there is no UpdateTarget method in Silverlight (as far as I know), so this is why this question was brought up.
If i understand you correctly, when the property changes, you should store the previous value of the SelectedItem backing field on an atrribute on your view model, and when the user decides to cancel the confirmation dialog, you should restore the SelectedItem backing field to the previous value.
If you're using regular properties for the backing field, you can write a Set method that implements this behavior:
private object selectedItemPreviousValue;
private object selectedItemBackingField;
public object SelectedItemBackingField
{
get
{
return selectedItemBackingField;
}
set
{
selectedItemPreviousValue = selectedItemBackingField;
selectedItemBackingField = value;
}
}
If you're using dependencie properties, you have to provide a PropertyMetada with a DependencyPropertyChantged callback, something like this:
public object SelectedItemBackingField
{
get { return (object)GetValue(SelectedItemBackingFieldProperty); }
set { SetValue(SelectedItemBackingFieldProperty, value); }
}
// Using a DependencyProperty as the backing store for SelectedItemBackingField. This enables animation, styling, binding, etc...
public static readonly DependencyProperty SelectedItemBackingFieldProperty =
DependencyProperty.Register("SelectedItemBackingField", typeof(object), typeof(MapApp), new PropertyMetadata(new PropertyChangedCallback(OnSelectedItemChanged));
public static OnSelectedItemChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
{
MyViewModel vm = (MyViewModel)sender;
vm.selectedItemPreviousValue = args.OldValue;
}
Within a form I have a user control for each field being returned. The control consists of a label and a texblock within a stack panel. This control is part of a datatemplate that makes up my form which is comprised of a wrap panel which contains the user controls. My intent is when the form is rendered to evaluate the bound property returned in my model and if it null set the visibility of the control to collapsed. The intent is to only have fields rendered within the form that has data being returned. The wrap panel allows for the controls to stay inline vs allowing excess white space in the form.
My initial thought was to iterate through the List that is returned and if the property in the model is null set the visibility of the control to collapsed via a dependency property. A concern I have here is with performance as some forms have over 700 fields / properties.
I was curious to learn if anyone has done a similar approach or what approach they used to control the visibility of UIElements
Thanks in advance for any suggestions
We use Dependency Properties to determine visibility of controls. We do this in concert with our Authorization library. So in our xaml, the code looks something like this:
<ListBoxItem x:Name="About"
Content="About Us"
AuthLib:Authorization.Visibility="WebUser"
Margin="10,5,10,5" />
<ListBoxItem x:Name="Accounting"
Content="Work Order Acct"
AuthLib:Authorization.Visibility="Admin, Accounting,Finance"
Margin="10,5,10,5" />
Where WebUser is any authenticated user, and obviously Accounting/Finance/Admin roles have elevated privilages.
We've done this with dozens of calls on a page without any problem, but never hundreds. Might be worth a copy/paste to see how it goes.
In case it's worthwhile, here's the visibility property in our Auth library:
#region Visibility
public static string GetVisibility(UIElement obj)
{
return (string)obj.GetValue(VisibilityProperty);
}
public static void SetVisibility(UIElement obj, string value)
{
obj.SetValue(VisibilityProperty, value);
}
/// Using a DependencyProperty as the backing store for requiresRole. This enables animation, styling, binding, etc...
public static readonly DependencyProperty VisibilityProperty = DependencyProperty.RegisterAttached(
"Visibility",
typeof(string),
typeof(Authorization),
new PropertyMetadata(Visibility_Callback));
// This callback will be invoked when some control will receive a value for your 'Visibility' property
private static void Visibility_Callback(DependencyObject source, DependencyPropertyChangedEventArgs e)
{
var uiElement = (UIElement)source;
if (App.IsAuthenticated)
{
RecalculateControlVisibility(uiElement);
}
else
{
EventHandler eh = null;
eh = delegate
{
RecalculateControlVisibility(uiElement);
};
App.Authenticated += eh;
RecalculateControlVisibility(uiElement);
}
}
private static void RecalculateControlVisibility(UIElement control)
{
//Authorization.UserHasRole() - is your code to check roles
if (Authorization.UserHasRole(GetVisibility(control)))
{
control.Visibility = Visibility.Visible;
}
else
{
control.Visibility = Visibility.Collapsed;
}
}
#endregion