WPF:如何使 DataGrid 与动态列的绑定可编辑?
我需要将一些数据绑定到具有可变列数的 DataGrid。我使用以下代码使其工作:
int n = 0;
foreach (string title in TitleList)
{
DataGridTextColumn col = new DataGridTextColumn();
col.Header = title;
Binding binding = new Binding(string.Format("DataList[{0}]", n++));
binding.Mode = BindingMode.TwoWay;
col.Binding = binding;
grid.Columns.Add(col);
}
其中 DataList 声明为:
public ObservableCollection<double> DataList { get; set; }
且 TitleList 声明为:
public ObservableCollection<string> TitleList { get; set; }
问题是,即使我指定了 TwoWay 绑定,它实际上是单向的。当我单击单元格尝试编辑时,出现异常“此视图不允许'EditItem'”。我是否错过了绑定表达式中的某些内容?
PS我找到了黛博拉的一篇文章 “使用 MVVM 在 Silverlight 应用程序中使用动态列填充 DataGrid”。但是,我很难让它适合我的情况(具体来说,我无法使标头绑定工作)。即使它有效,我仍然面临诸如单元格样式不一致之类的问题。这就是为什么我想知道是否可以让我的上面的代码工作——稍微调整一下?
编辑:我发现另一篇文章可能与我的问题有关: 隐式双向绑定。看起来如果您使用将字符串列表绑定到 TextBox,
<TextBox Text="{Binding}"/>
您将收到类似“双向绑定需要 Path 或 XPath”的错误。但是这个问题可以很容易地通过使用来解决
<TextBox Text="{Binding Path=DataContext, RelativeSource={RelativeSource Self}}"/>
,或者
<TextBox Text="{Binding .}"/>
任何人都可以给我一个提示,如果我的问题可以用类似的方式解决吗?
I need to bind some data to a DataGrid with variable number of columns. I made it work using following code:
int n = 0;
foreach (string title in TitleList)
{
DataGridTextColumn col = new DataGridTextColumn();
col.Header = title;
Binding binding = new Binding(string.Format("DataList[{0}]", n++));
binding.Mode = BindingMode.TwoWay;
col.Binding = binding;
grid.Columns.Add(col);
}
where DataList is declared as:
public ObservableCollection<double> DataList { get; set; }
and TitleList is declared as:
public ObservableCollection<string> TitleList { get; set; }
The problem is that, even though I specified TwoWay binding, it is really one-way. When I click a cell to try to edit, I got an exception "'EditItem' is not allowed for this view". Did I just miss something in the binding expression?
P.S. I found an article from Deborah "Populating a DataGrid with Dynamic Columns in a Silverlight Application using MVVM". However, I had hard time to make it work for my case (specifically, I can't make the header binding work). Even if it worked, I'm still facing issues like inconsistent cell styles. That's why I'm wondering if I could make my above code work - with a little tweak?
EDIT: I found another post which might be related to my problem: Implicit Two Way binding. It looks if you bind to a list of string to a TextBox using
<TextBox Text="{Binding}"/>
You will get an error like "Two-way binding requires Path or XPath". But the problem can easily be fixed by using
<TextBox Text="{Binding Path=DataContext, RelativeSource={RelativeSource Self}}"/>
or
<TextBox Text="{Binding .}"/>
Can anybody give me a hint if my problem can be solved in a similar way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您绑定到索引器吗?您可以向我们展示您的 DataList 属性是什么样子吗?
我不久前对索引属性也做了同样的事情。
编辑:您无法编辑属性的原因是您尝试编辑“双字段”。
一种解决方法是将您的双精度数包装到带有 INotifyPropertyChanged 的类中。
那么你可以使用 a
并且可以编辑你的值。索引是否始终相同的问题仍然存在:)
编辑2:工作示例:只是为了显示twoway正在工作
double的包装器:
的用户控件
带有datagrid.cs
我希望您通过这个示例找到正确的方向。
Do you bind to an indexer?. can you show us how your DataList Property looks like?
i did the same a while ago with an indexed property.
EDIT: the reason that you cant edit your Property, is that you try to edit a "double field".
one workaround would be to wrap your double into a class with INotifyPropertyChanged.
then you can use a
and you can edit your value. the question wether the index are always the same stay still around :)
EDIT2: working example: just to show twoway is working
Wrapper for double:
usercontrol with a datagrid
.cs
i hope you get in the right direction with this example.