WPF:如何使数据网格与动态列可编辑绑定?
我需要一些数据绑定到的列的数目可变的一个DataGrid。我做了它的工作用以下code:
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);
}
在这里DataList的声明为:
where DataList is declared as:
public ObservableCollection<double> DataList { get; set; }
和TitleList声明为:
and TitleList is declared as:
public ObservableCollection<string> TitleList { get; set; }
问题是,即使我指定的双向绑定,实在是单向的。当我点击一个单元格尝试编辑,我得到了一个异常'EditItem'不允许这种观点。难道我只是想念在绑定前pression的东西吗?
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。我发现从德博拉使用MVVM填充与动态列一个DataGrid的Silverlight应用程序。不过,我有困难时期,使之成为我的情况下工作(具体地讲,我不能让头绑定工作)。即使它的工作,我仍然面临着像不一致的单元格样式的问题。这就是为什么我想知道如果我可以让我的上述code的工作 - 一个小调整?
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?
编辑:我发现了另一个帖子里面可能与我的问题:的隐式双向绑定。看起来如果使用绑定到字符串的列表,以一个TextBox
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}"/>
您会得到像双向绑定需要路径或XPath错误。但问题可以很容易地通过使用固定
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}}"/>
或
<TextBox Text="{Binding .}"/>
任何人都可以给我一个提示,如果我的问题可以用类似的方式来解决?
Can anybody give me a hint if my problem can be solved in a similar way?
你绑定到一个索引?你能告诉我们你的DataList控件属性怎么样子?
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.
public SomeObjectWithIndexer DataList
{get; set;}
public class SomeObjectWithIndexer
{
public string this
{
get { ... }
set { ... }//<-- you need this one for TwoWay
}
}
编辑:你不能编辑属性的原因,是你尝试编辑双田。
一个解决办法将是包装你的双与INotifyPropertyChanged的一类。
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.
public class DataListItem
{
public double MyValue { get; set;}//with OnPropertyChanged() and stuff
}
那么你可以使用
ObservableCollection<DataListItem>
您可以编辑你的价值。阉指数的问题都是一样的住宿仍然存在:)
and you can edit your value. the question wether the index are always the same stay still around :)
Binding binding = new Binding(string.Format("DataList[{0}].MyValue", n++));
EDIT2:工作示例:只是为了显示双向正在
working example: just to show twoway is working
public class DataItem
{
public string Name { get; set; }
public ObservableCollection<DataListItem> DataList { get; set; }
public DataItem()
{
this.DataList = new ObservableCollection<DataListItem>();
}
}
包装双:
public class DataListItem
{
private double myValue;
public double MyValue
{
get { return myValue; }
set { myValue = value; }//<-- set breakpoint here to see that edit is working
}
}
用户控件与一个DataGrid
usercontrol with a datagrid
<UserControl x:Class="WpfStackoverflow.IndexCollectionDataGrid"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<DataGrid ItemsSource="{Binding MyList}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Binding="{Binding Path=Name}" />
<DataGridTextColumn Header="Index1" Binding="{Binding Path=DataList[0].MyValue, Mode=TwoWay}" />
<DataGridTextColumn Header="Index2" Binding="{Binding Path=DataList[1].MyValue, Mode=TwoWay}" />
</DataGrid.Columns>
</DataGrid>
</UserControl>
的.cs
public partial class IndexCollectionDataGrid : UserControl
{
public IndexCollectionDataGrid()
{
InitializeComponent();
this.MyList = new ObservableCollection<DataItem>();
var m1 = new DataItem() {Name = "test1"};
m1.DataList.Add(new DataListItem() { MyValue = 10 });
m1.DataList.Add(new DataListItem() { MyValue = 20 });
var m2 = new DataItem() { Name = "test2" };
m2.DataList.Add(new DataListItem() { MyValue = 100 });
m2.DataList.Add(new DataListItem() { MyValue = 200 });
this.MyList.Add(m1);
this.MyList.Add(m2);
this.DataContext = this;
}
public ObservableCollection<DataItem> MyList { get; set; }
}
我希望你在这个例子中,正确的方向得到。
i hope you get in the right direction with this example.