且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用WPF数据绑定复杂对象模型

更新时间:2023-10-02 09:19:40

我没有看到你的方法有问题,你是基本上创建一个ViewModel来包装权限。



然而,我将进一步说明一个可选的ViewModel,这将是任何其他CheckBox-您可能拥有的基于选择的屏幕:

  public class可选< T>:ViewModelBase // ViewModelBase应实现NotifyPropertyChanged。 
{
private T _model;
public T模型
{get {return _model; }
set
{
_model = value;
NotifyPropertyChange(Model);
}
}

private bool _isSelected;
public bool IsSelected
{
get {return _isSelected; }
set
{
_isSelected = value;
NotifyPropertyChange(IsSelected);
}
}

}

然后你可以做:

  public ObservableCollection<可选< Permission>>权限{get; set } //等。 


I'm currently working on refactoring some old code that was written on top of WPF with C#.

I currently have a composite user model that looks like this:

public class UserModel{
   public User User {get; set;}
   public List<Guid> Permissions {get; set;}
}

In this model the Permissions list is a list of the permission IDs granted to the User referenced by the model.

In my xaml for my user management screen I have a list of these models bound to a combo box and when the selection changes the selected item is bound to a Grid containing text boxes for the User property and an Items Control.

This items control is filled with check boxes bound to all of the permissions in the systems when the page loads. My question is what is the best way to bind the Permissions list to the check boxes in the items control?

The code for the items control looks something like this:

<ScrollViewer Grid.Row="7" Grid.Column="1">
  <ItemsControl ItemsSource={Binding}>
    <ItemsControl.ItemsTemplate>
        <DataTemplate>
            <Checkbox Content="{Binding Description}" IsChecked="{Binding IsSelected}"/>
        </DataTemplate>
    </ItemsControl.ItemsTemplate>
</ScrollViewer>

And the collection of permissions is a List of this object:

class SelectedPermission{
   public Guid PermissionId {get; set;}
   public string Description {get; set;}
   public bool IsSelected {get; set;}
 }

My initial thought was to implement INotifyPropertyChanged on the SelectedPermission collection and when I bind my user just set IsSelected to true for any permissions that are granted to the selected user. But it seems like there should be a better way.

I don't see anything wrong with your approach, you're basically creating a ViewModel to wrap the permissions into.

However, I would go a step further and define a Selectable ViewModel, which would be a generic solution for whatever other CheckBox-based selection screens you might have:

public class Selectable<T>: ViewModelBase //ViewModelBase should Implement NotifyPropertyChanged.
{
    private T _model;
    public T Model 
    {   get { return _model; }
        set 
        {
            _model = value;
            NotifyPropertyChange("Model");
        }
    }

    private bool _isSelected;
    public bool IsSelected
    {
        get { return _isSelected; }
        set
        {
            _isSelected = value;
            NotifyPropertyChange("IsSelected");
        }
    }

 }

Then you can do:

public ObservableCollection<Selectable<Permission>> Permissions {get;set; } //etc.