Click here to Skip to main content
14,271,013 members

Silverlight / WPF

 
GeneralRe: Assign Collection Items In XAML Pin
Kevin Marois23-Apr-18 9:25
professionalKevin Marois23-Apr-18 9:25 
AnswerRe: Assign Collection Items In XAML Pin
Maciej Los23-Apr-18 9:56
protectorMaciej Los23-Apr-18 9:56 
GeneralRe: Assign Collection Items In XAML Pin
Kevin Marois23-Apr-18 10:00
professionalKevin Marois23-Apr-18 10:00 
GeneralRe: Assign Collection Items In XAML Pin
Richard Deeming24-Apr-18 1:14
mveRichard Deeming24-Apr-18 1:14 
QuestionMouseEventArgs.CLicks in WPF Pin
Enobong Adahada20-Apr-18 6:33
memberEnobong Adahada20-Apr-18 6:33 
AnswerRe: MouseEventArgs.CLicks in WPF Pin
Gerry Schmitz20-Apr-18 13:18
mveGerry Schmitz20-Apr-18 13:18 
GeneralRe: MouseEventArgs.CLicks in WPF Pin
Enobong Adahada21-Apr-18 13:08
memberEnobong Adahada21-Apr-18 13:08 
QuestionField Level Rights Pin
Kevin Marois18-Apr-18 7:38
professionalKevin Marois18-Apr-18 7:38 
I want to implement user access rights at the field level. This is what I've come up with so far. It works well but I have one drawback:

Code Behind
public partial class MainWindow : Window, INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    // Dictionary of rights
    private Dictionary<string, bool> _Rights;
    public Dictionary<string, bool> Rights
    {
        get { return _Rights; }
        set
        {
            if (_Rights != value)
            {
                _Rights = value;
                RaisePropertyChanged("Rights");
            }
        }
    }

    // Bound data. Could be defined here or on a model
    private double _CreditLimit;
    public double CreditLimit
    {
        get { return _CreditLimit; }
        set
        {
            if (_CreditLimit != value)
            {
                _CreditLimit = value;
                RaisePropertyChanged("CreditLimit");
            }
        }
    }

    public MainWindow()
    {
        InitializeComponent();

        this.DataContext = this;

        // The property bound to the textbox's Test property
        CreditLimit = 125000;

        // Dictionary of rights
        Rights = new Dictionary<string, bool>();
        Rights.Add("creditLimit", false); // The user cannot access the Credit Limit textbox
    }

    private void RaisePropertyChanged(string propertyName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

XAML
<TextBox Text="{Binding CreditLimit, Mode=TwoWay}"
            IsEnabled="{Binding Rights[creditLimit]}"
            Width="100"
            HorizontalAlignment="Left"
            Margin="10"/>

The rights, and therefore access to the field, could be changed at runtime by simply doing
Rights["creditLimit"] = true;
RaisePropertyChanged("Rights");

The only drawback is that you really can't run any logic, meaning run specific code to determine if a textbox can be enabled. For example, the requirements for enabling the Credit Limit field might be
  1. The user has rights to modify the credit limit
  2. The customer is in good standing (their credit is still good)
  3. The customer does not have a balance
In this case I would not only want to check the rights list, but also some conditions on the Customer model. Where would this code go?

It is possible to somehow run a method on the VM from the IsEnabled property in the XAML?

I could try to use a multiinding but this puts the business logic in the XAML and would be messy and hard to maintain.
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.

AnswerRe: Field Level Rights Pin
Gerry Schmitz18-Apr-18 7:45
mveGerry Schmitz18-Apr-18 7:45 
GeneralRe: Field Level Rights Pin
Kevin Marois18-Apr-18 8:14
professionalKevin Marois18-Apr-18 8:14 
GeneralRe: Field Level Rights Pin
Gerry Schmitz18-Apr-18 8:43
mveGerry Schmitz18-Apr-18 8:43 
GeneralRe: Field Level Rights Pin
Kevin Marois18-Apr-18 8:54
professionalKevin Marois18-Apr-18 8:54 
GeneralRe: Field Level Rights Pin
Gerry Schmitz18-Apr-18 9:50
mveGerry Schmitz18-Apr-18 9:50 
AnswerRe: Field Level Rights Pin
Mycroft Holmes18-Apr-18 14:31
memberMycroft Holmes18-Apr-18 14:31 
QuestionApplication.Run C# equivalent Pin
GregJ717-Apr-18 4:32
memberGregJ717-Apr-18 4:32 
AnswerRe: Application.Run C# equivalent Pin
Richard MacCutchan17-Apr-18 4:38
protectorRichard MacCutchan17-Apr-18 4:38 
GeneralRe: Application.Run C# equivalent Pin
GregJ717-Apr-18 6:26
memberGregJ717-Apr-18 6:26 
GeneralRe: Application.Run C# equivalent Pin
Richard MacCutchan17-Apr-18 6:38
protectorRichard MacCutchan17-Apr-18 6:38 
GeneralRe: Application.Run C# equivalent Pin
GregJ717-Apr-18 7:59
memberGregJ717-Apr-18 7:59 
GeneralRe: Application.Run C# equivalent Pin
Richard MacCutchan17-Apr-18 8:47
protectorRichard MacCutchan17-Apr-18 8:47 
GeneralRe: Application.Run C# equivalent Pin
GregJ717-Apr-18 8:04
memberGregJ717-Apr-18 8:04 
GeneralRe: Application.Run C# equivalent Pin
Richard MacCutchan17-Apr-18 8:48
protectorRichard MacCutchan17-Apr-18 8:48 
GeneralRe: Application.Run C# equivalent Pin
Richard Deeming19-Apr-18 1:52
mveRichard Deeming19-Apr-18 1:52 
GeneralRe: Application.Run C# equivalent Pin
GregJ719-Apr-18 2:42
memberGregJ719-Apr-18 2:42 
AnswerRe: Application.Run C# equivalent Pin
GregJ717-Apr-18 11:52
memberGregJ717-Apr-18 11:52 

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.