wpf excel 喜欢网格编辑吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21614090/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
wpf excel like grid editing?
提问by Stefano.net
In a project I have a very tricky requirement I don't know how to solve:
在一个项目中,我有一个非常棘手的要求,我不知道如何解决:
I have several datagrids in a single wpf window (I use MVVM) all binded to some collection in the linked ViewModel. The customer wants to edit each of these grids, either within the grid or in a common textbox (like in excel).
我在单个 wpf 窗口(我使用 MVVM)中有几个数据网格,它们都绑定到链接的 ViewModel 中的某个集合。客户想要编辑这些网格中的每一个,无论是在网格内还是在公共文本框中(如在 excel 中)。
I'm banging the head on how to do the latter. What I would do is bind the textbox with a property in the viewmodel, but when the value is changed there, I need to change the value in the original property binded with the datagrid cell accordingly. In other words, I need to know what collection and which property of that collection I need to change with the data in the textbox accordingly .
我正在研究如何做后者。我要做的是将文本框与视图模型中的一个属性绑定,但是当那里的值发生变化时,我需要相应地更改与数据网格单元绑定的原始属性中的值。换句话说,我需要知道我需要根据文本框中的数据相应地更改哪个集合和该集合的哪个属性。
I tried several ways but with no luck.
我尝试了几种方法,但没有运气。
Reflection? DependencyProperty? What else?
反射?依赖属性?还有什么?
Any help?
有什么帮助吗?
Thank you
谢谢
回答by Matt
Assuming that you're using the built-in WPF DataGrid, you'll need to setup your grid similarly:
假设您使用的是内置 WPF DataGrid,您需要类似地设置您的网格:
<DataGrid SelectionUnit="Cell" SelectionMode="Single" ItemsSource="{Binding Data}" SelectedCellsChanged="DataGrid_OnSelectedCellsChanged">
...
</DataGrid>
Also give your TextBoxa name:
也给你TextBox一个名字:
<TextBox x:Name="textBox" DockPanel.Dock="Top" />
In the code-behind, you'll need to manually wire up this event, since apparently the DataGriddoesn't allow you to bind to the selected item/cell/value when using SelectionUnit="Cell":
在代码隐藏中,您需要手动连接此事件,因为显然DataGrid不允许您在使用时绑定到所选项目/单元格/值SelectionUnit="Cell":
private void DataGrid_OnSelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e)
{
if (e.AddedCells.Count == 0)
this.textBox.SetBinding(TextBox.TextProperty, (string) null);
else
{
var selectedCell = e.AddedCells.First();
// Assumes your header is the same name as the field it's bound to
var binding = new Binding(selectedCell.Column.Header.ToString())
{
Mode = BindingMode.TwoWay,
Source = selectedCell.Item,
UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
};
this.textBox.SetBinding(TextBox.TextProperty, binding);
}
}
I tried getting this done without code-behind but after looking around it didn't seem like this was possible.
我尝试在没有代码隐藏的情况下完成这项工作,但环顾四周后,这似乎是不可能的。
回答by Stefano.net
In addition to tencntraze answer I used this code to get te property bound to the cell
除了tencntraze回答我使用此代码将te属性绑定到单元格
var property = (selectedCell.Column.ClipboardContentBinding as Binding).Path.Path;

