gpt4 book ai didi

c# - WPF 数据网格和 ITypedList

转载 作者:行者123 更新时间:2023-11-30 17:04:55 25 4
gpt4 key购买 nike

我尝试在我的 ItemsSource 中实现 ITypedList,但从未调用 PropertyDescriptor.GetValue/SetValue。这有什么问题吗?

XAML 看起来像这样:

<Window x:Class="WpfApplication5.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:local="clr-namespace:WpfApplication5"
Title="MainWindow" Height="350" Width="525">
<Grid>
<DataGrid AutoGenerateColumns="True">
<DataGrid.ItemsSource>
<local:RowCollection/>
</DataGrid.ItemsSource>
</DataGrid>
</Grid>
</Window>

RowCollection 定义为:

public class RowCollection : ReadOnlyCollection<Row>, ITypedList {
readonly PropertyDescriptorCollection _properties;

public RowCollection() : base(new List<Row>()) {
_properties = new PropertyDescriptorCollection(new[] {
new RowDescriptor("Name"),
new RowDescriptor("Value")
}, true);

Items.Add(new Row());
Items.Add(new Row());
}

PropertyDescriptorCollection ITypedList.GetItemProperties(PropertyDescriptor[] listAccessors) {
return _properties;
}

string ITypedList.GetListName(PropertyDescriptor[] listAccessors) {
return null;
}
}

RowDescriptor 在哪里:

public class RowDescriptor : PropertyDescriptor {
public RowDescriptor(string name)
: base(name, new Attribute[0]) {
}

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

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

public override object GetValue(object component) {
var row = (Row)component;
return row[Name];
}

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

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

public override void ResetValue(object component) {
throw new NotSupportedException();
}

public override void SetValue(object component, object value) {
var row = (Row)component;
row[Name] = String.Format("{0}", value ?? String.Empty);
}

public override bool ShouldSerializeValue(object component) {
return false;
}
}

行是:

public class Row {        
readonly Dictionary<string, string> _values;

public Row() {
_values = new Dictionary<string, string>();
this["Name"] = "Foo";
this["Value"] = "Bar";
}

public string this[string name] {
get {
if (!_values.ContainsKey(name))
return String.Empty;

return _values[name];
}
set {
_values[name] = value;
}
}
}

最佳答案

关于c# - WPF 数据网格和 ITypedList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16929720/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com