WPF Datagrid Databind类包含静态属性和包含动态属性值条目的字典

时间:2011-05-24 00:25:58

标签: c# wpf datagrid propertydescriptor icustomtypedescriptor

已更新

我正在更新这篇文章,因为我做了一些阅读并决定重新实施我的解决方案。

原始问题:我有一个具有静态属性的类和一个属性的动态集合属性(通过字典)。我想将我的类数据绑定到一个wpf数据网格,其中每个静态属性应该是一列,每个字典条目应该是网格中的一列。

在做了一些研究之后,我决定实现一个PropertyBag类,它将包含我的属性和值的Dictionary。现在几乎一切都在运转。我的网格显示所有正确的列,并且正确应用静态属性值。

但是,现在我无法将字典中的任何值应用到网格中,我不知道从哪里开始。

更多信息:

我的数据库有3个表,一个牌子,一个类别和一个categoryplateassociation表。每个板可以有0到多个类别。现在,我正在使用所有类别填充每个板块并将字符串设置为空。然后,当返回关联(在牌照和类别之间)时,我在特定类别名称上设置实际值。这一切都发生在网格创建之前。

物业袋:

public class PropertyBag
{
    private readonly Dictionary<string, string> values = new Dictionary<string, string>();

    public string this[string key]
    {
        get 
        {
            string value;
            values.TryGetValue(key, out value);
            return value;
        }
        set
        {
            if (value == null) values.Remove(key);
            else values[key] = value;
        }
    }
}

修订了Plate class

[TypeDescriptionProvider(typeof(PlateTypeDescriptionProvider))]
public class Plate : INotifyPropertyChanged
{
    public int ID;
    private string name;
    private string status;
    private string creator;
    private Uri location;
    private string description;

    public Plate()
    {
        CustomCategories = new PropertyBag();
    }

    public PropertyBag CustomCategories { get; set; }

    public string Name
    {
        get { return name;}
        set
        {
            name = value;
            NotifyPropertyChanged("Name");
        }
    }

    public string Status
    {
        get { return status; }
        set
        {
            status = value;
            NotifyPropertyChanged("Status");
        }
    }

    public string Creator
    {
        get { return creator; }
        set
        {
            creator = value;
            NotifyPropertyChanged("Creator");
        }
    }

    public Uri Location
    {
        get { return location; }
        set
        {
            location = value;
            NotifyPropertyChanged("Location");
        }
    }

    public string Description
    {
        get { return description; }
        set
        {
            description = value;
            NotifyPropertyChanged("Description");
        }
    }

    public static Plate ConvertDataPlateToBusinessPlate(TestPlate dataPlate)
    {
        var plate = new Plate
                        {
                            Name = dataPlate.Name, 
                            Status = dataPlate.Status,
                            Creator = dataPlate.Creator, 
                            Description = dataPlate.Description, 
                            Location = new Uri(dataPlate.Location)
                        };
        return plate;
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

修改了CustomTypeDescriptor:

public override PropertyDescriptorCollection GetProperties()
    {
        return GetProperties(null);
    }

    public override PropertyDescriptorCollection GetProperties(Attribute[] attributes)
    {
        var properties = new ArrayList();
        foreach (PropertyDescriptor propertyDescriptor in base.GetProperties(attributes))
        {
            if(propertyDescriptor.PropertyType.Equals(typeof(PropertyBag)))
            {
                //Static list of all category names
                var categoryNames = Categories.GetAll();
                foreach (var categoryName in categoryNames)
                {
                    properties.Add(new PropertyBagPropertyDescriptor(categoryName));
                }
            }
            else
            {
                properties.Add(propertyDescriptor);
            }

        }
        var props = (PropertyDescriptor[])properties.ToArray(typeof(PropertyDescriptor));
        return new PropertyDescriptorCollection(props);
    }

修订后的PropertyDescriptor

    public class PropertyBagPropertyDescriptor : PropertyDescriptor
{
    public PropertyBagPropertyDescriptor(string name) : base(name, null)
    {}

    public override bool CanResetValue(object component)
    {
        return true;
    }

    public override object GetValue(object component)
    {
        return ((PropertyBag) component)[Name];
    }

    public override void ResetValue(object component)
    {
        ((PropertyBag)component)[Name] = null;
    }

    public override void SetValue(object component, object value)
    {
        ((PropertyBag) component)[Name] = (string) value;
    }

    public override bool ShouldSerializeValue(object component)
    {
        return ((PropertyBag)component)[Name] != null;
    }

    public override Type ComponentType
    {
        get { return typeof(PropertyBag); }
    }

    public override bool IsReadOnly
    {
        get { return false; }
    }

    public override Type PropertyType
    {
        get { return typeof(string); }
    }
}

简单的ViewModel

 public TestPlateAdministratorViewModel()
    {
        CommandAggregator = new TestPlateAdministratorCommandAggregator(this);
        LoadData();
    }

    public static TestPlateAdministratorCommandAggregator CommandAggregator { get; set; }
    public ObservableCollection<Plate> TestPlates{ get; set; }

    private static void LoadData()
    {
        CommandAggregator.LoadPlatesCommand.Execute(null);
        CommandAggregator.LoadCategoriesCommand.Execute(null);
    }
}

1 个答案:

答案 0 :(得分:0)

您的PropertyBag中的词典是否具有固定大小或键是否已知?

你没有为你的数据网格发布你的xaml,但是从propertybag到一列的绑定可能如下所示:

<DataGridTextColumn Header="Col4TestKey" Binding="{Binding CustomCategories[test]}"/>

我真的不知道你的PropertyBag setter是否适用于绑定。总而言之,如果你有一套已知的词典键,那么这就行了。

顺便说一下,我在我的项目中使用平面数据表来获得这些动态的东西,这真的很容易处理。