stackpanel visibility editing in silverlight

I am working on silverlight web application (RIA) hosted on to a silverlight web application.

Background: I have a stackpanel in one of the xaml pages. I tried to simplify my problem. It is down as follows:

My xaml page has a stack panel and that stackpanel has only textblock and button in it. only authorized users can see the value in the textblock, when navigated to the xaml page. If the user is unauthorized, I am planning to show "unauthorised" message (instead of value) in the textblock (button is also collapsed. I dont show button.)

FYI, this value of texblock comes from a WCF service and i have made proper attributes for authorization in the service. If unauthorized, value will not come to textblock from service. What I am doing now, is just for UI, when some user tries to enter the url of the page from browser's address bar I want to show him the unauthorised message.

What have I done so far:: I gave a name to stackpanel. I gave a name to textblock and a name to button. if authorized, I am setting the text value of text block to the service returned value.

If unauthorized, I am changing the visibility and text from .cs file as this.

textblock.Text="unauthorized";
buttonname.Visibility = Visibility.Collapsed;

Problem: Problem is I got an idea. :( When I have a name for my stackpanel, I can say stackpanelname.Visibility = Visibility.Collapsed;

But I cannot show my message (Textblock) now, coz child controls are also collapsed with parent. Is there any way that, I can collapse my stackpanel (parent control), but only one control of it (one child control, or few) is visible.

Solutions: As I am typing this, I got a solution, I can go for a new stack panel, with a text block, whose visibility I can set to VISIBLE and text as my message. But, is it the only way.? How do we handle these situations?

In general, presuming my question is clear, what is general approach to provide security in this kind of scenarios, from UI. Is it gonna be redirecting to error page/login page or a message to user on the same page or any other thing. Presuming, Backend services, will be having attributes and logic for authorizations.

Answers


Normally I would solve such an issue with bindings. I'm not sure if you're willing to take that approach, but given that you are. For example, i would do this:

<TextBlock Text="{Binding AuthorizationCaption,FallbackValue='Unauthorized'" />

The fallback value here is used to see what it looks like for design purposes. FallbackValues only get presented when a binding fails.

For the button, I would do this:

<Button Content="My Button" Visibility="{Binding IsAuthorized,Converter={StaticResource VisibilityValueConverter},FallbackValue=Collapsed}" />

In the above example, you will notice that I used what I called a VisibilityValueConverter. This of course would be a part of my resources that I define right about "LayoutRoot" in the XAML. e.g.

<UserControl.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/MyUICustomizationLib;component/rdMyResourceDictionary.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</UserControl.Resources>

My VisibilityValueConverter is a simple converter to convert a boolean value to a visibility attribute. It's pretty reusable. It implements IValueConverter which I think is in System.Windows.Data. Along the lines:

public class TVisibilityValueConverter:IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        bool _visibility = (bool)value;
        return _visibility ? Visibility.Visible : Visibility.Collapsed;
    }
    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        Visibility _visibility = (Visibility)value;
        return _visibility == Visibility.Visible;
    }
}

I would embed my value converter in my ResourceDictionary by , first, defining a namespace to it's location -

xmlns:muicl="clr-namespace:MyUICustomizationLib.ValueConverters"

Then defining the static resource:

<muicl:TVisibilityValueConverter x:Key="VisibilityValueConverter" />

Last, but not least, Implement my viewmodel properties and commands to fit what has to be. i.e, I make the neccesary server calls, and on the call backs, i toggle the AuthorizationCaption property and the IsAuthorized property accordingly. The smmoothest way would be to let the AuthorizationCaption be a simple if statement that depends on the IsAuthorized property. Let the set method of the IsAuthorizedProperty raise an event notification that the AuthorizationCaption changed too. e.g.

set
{
    __fIsAuthorized = value;//set private member
    this.RaisePropertyChanged("IsAuthorized");
    this.RaisePropertyChanged("AuthorizationCaption");
}

Of course they may be more to set up on your end. But this would be the route I would take.


Need Your Help

how to detect the first page on jsPDF

javascript jquery css jspdf

how can I detect/condition on the first page on jsPDF??

Encrypt Request/Reponse in MVC4 WebApi

asp.net-mvc rest asp.net-web-api

I have a old api that receives an encrypted request and encrypts the response once complete. I am attempting to switch this to mvc4 webapi and it has gone smooth until I hit this encryption. What ...