0

using Entity Framework (C#) I have a User class which has ONE:MANY mapping to the UserRight class (simply, user has a set of rights). Each right is identified by a string. And now, because the maximum number of possible rights is finite (<10) I'd like to have 10 CheckBoxes and edit the subset of rights for a given user manually.

What is the nice way to do it?

James

1
  • checkboxes gives you a bool, your userrights are strings - so what you wanna do exactly? Commented May 25, 2011 at 8:28

1 Answer 1

2

Create a RightViewModel class to contain user rights:

public class RightViewModel : INotifyPropertyChanged
{
    private string _name;
    public string Name
    {
        get { return _name; }
        set
        {
            _name = value;
            Change("Name");
        }
    }

    private bool _hasRight;
    public bool HasRight
    {
        get { return _hasRight; }
        set
        {
            _hasRight = value;
            Change("HasRight");
        }
    }

    public void Change(string strPropertyName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(strPropertyName));
    }
    public event PropertyChangedEventHandler PropertyChanged;
}

Create a similar class for your user, containing a member Rightsof type ObservableCollection<RightViewModel>.

In you XAML, use an ItemsControl:

<ItemsControl ItemsSource="{Binding Rights}"
    ItemTemplate="{StaticResource RightTemplate}"/>

And a template definition:

<DataTemplate x:Key="RightTemplate">
    <CheckBox Content="{Binding Name}" IsChecked="{Binding HasRight, Mode=TwoWay}"/>
</DataTemplate>

Mode=TwoWay makes the binding update your RightViewModel instance.

Define the ItemsControl's ItemsPanel if you need to display your checkboxes with a different layout.

Finally set your user as the DataContext of your container.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.