WPF:A DataGrid同步B DataGrid与columnWith和DisplayIndex [英] WPF : A DataGrid sync B DataGrid With columnWith and DisplayIndex
问题描述
我使用了此链接 http: //thibaud60.blogspot.com/2010/02/footer-on-wpf-datagrid-by-use-several.html 来同步两个DataGrid,但拖放列标题有时会出现错误。
i used this link http://thibaud60.blogspot.com/2010/02/footer-on-wpf-datagrid-by-use-several.html to sync two DataGrid,but the Drag and drop column header sometime has bug.
DataGrid具有三列:A1,A2,A3
A DataGrid has three column: A1,A2,A3
B DataGrid具有三列:B1,B2,B3
B DataGrid has three column: B1,B2,B3
I SetBinding像这样:
I SetBinding like:
Binding bindingWidth = new Binding();
bindingWidth.Mode = BindingMode.TwoWay;
bindingWidth.Source = A1;
bindingWidth.Path = new PropertyPath(DataGridColumn.WidthProperty);
BindingOperations.SetBinding(B1, DataGridColumn.WidthProperty, bindingWidth);
Binding bindingDisplayIndex = new Binding();
bindingDisplayIndex.Mode = BindingMode.TwoWay;
bindingDisplayIndex.Source = A1;
bindingDisplayIndex.Path = new PropertyPath(DataGridColumn.DisplayIndexProperty);
BindingOperations.SetBinding(B1, DataGridColumn.DisplayIndexProperty, bindingDisplayIndex);
通常这项工作很好,
但是我有时通过拖动列来更改A1 displayindex的双向绑定不起作用:
but i changed A1 displayindex by drag the column sometime the two way binding is not work:
我将A3拖动到A1位置不成功,但是B3将成功将位置更改为B1
i drag A3 TO A1 position is not success , but B3 will success changed position to B1
A1.DisplayIndex!= B1.DisplayIndex吗?
the A1.DisplayIndex != B1.DisplayIndex ?
为什么会这样?
推荐答案
我花了一些时间找到好的方法,所以对于那些不想搜索的人:-)
谢谢您的帮助
I took some times to find the good approach, so for those who don't want search :-) Thank you for your help
private static Dictionary<DataGridColumn, DataGridColumn> ColumnCache = new Dictionary<DataGridColumn, DataGridColumn>();
private static void SynchronizeVerticalDataGrid(DataGrid source, DataGrid associated)
{
associated.HeadersVisibility = source.HeadersVisibility & (DataGridHeadersVisibility.Row);
associated.HorizontalScrollBarVisibility = ScrollBarVisibility.Hidden;
int sourceColIndex = 0;
for (int associatedColIndex = 0; associatedColIndex < associated.Columns.Count; associatedColIndex++)
{
var colAssociated = associated.Columns[associatedColIndex];
var colSource = source.Columns[sourceColIndex];
sourceColIndex++;
ColumnCache.Add(colSource, colAssociated);
Binding binding = new Binding();
binding.Mode = BindingMode.TwoWay;
binding.Source = colSource;
binding.Path = new PropertyPath(DataGridColumn.WidthProperty);
BindingOperations.SetBinding(colAssociated, DataGridColumn.WidthProperty, binding);
}
source.ColumnDisplayIndexChanged += (sender1, e1) =>
{
var a = ColumnCache[e1.Column];
a.DisplayIndex = e1.Column.DisplayIndex;
};
}
这篇关于WPF:A DataGrid同步B DataGrid与columnWith和DisplayIndex的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!