使用 WPF 数据绑定复杂对象模型 [英] Databinding Complex Object Models with WPF

查看:13
本文介绍了使用 WPF 数据绑定复杂对象模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在重构一些使用 C# 在 WPF 之上编写的旧代码.

我目前有一个如下所示的复合用户模型:

公共类用户模型{公共用户用户{get;放;}公共列表<Guid>权限{get;放;}}

在此模型中,权限列表是授予模型引用的用户的权限 ID 列表.

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.

当页面加载时,此项目控件填充了绑定到系统中所有权限的复选框.我的问题是将权限列表绑定到项目控件中的复选框的最佳方法是什么?

items 控件的代码如下所示:

<ItemsControl ItemsSource={绑定}><ItemsControl.ItemsTemplate><数据模板><Checkbox Content="{Binding Description}" IsChecked="{Binding IsSelected}"/></数据模板></ItemsControl.ItemsTemplate></ScrollViewer>

而权限集合就是这个对象的List:

class SelectedPermission{公共 Guid PermissionId {get;放;}公共字符串 描述 {get;放;}public bool IsSelected {get;放;}}

我最初的想法是在 SelectedPermission 集合上实现 INotifyPropertyChanged,当我绑定我的用户时,只需将 IsSelected 设置为 true 以将授予所选用户的任何权限.但似乎应该有更好的方法.

解决方案

我看不出你的方法有什么问题,你基本上是在创建一个 ViewModel 来包装权限.

但是,我会更进一步,定义一个 Selectable ViewModel,这将是您可能拥有的任何其他基于 CheckBox 的选择屏幕的通用解决方案:

public class Selectable: ViewModelBase//ViewModelBase 应该实现 NotifyPropertyChanged.{私人 T _model;公共T型{ 得到 { 返回 _model;}放{_model = 值;NotifyPropertyChange("模型");}}私人布尔_isSelected;public bool IsSelected{得到 { 返回 _isSelected;}放{_isSelected = 值;NotifyPropertyChange("IsSelected");}}}

然后你可以这样做:

public ObservableCollection>权限{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.

这篇关于使用 WPF 数据绑定复杂对象模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆