我使用此链接http://thibaud60.blogspot.com/2010/02/footer-on-wpf-datagrid-by-use-several.html同步两个DataGrid,但拖放列标题有时会出错。
DataGrid有三列:A1,A2,A3
B DataGrid有三列:B1,B2,B3
我像SetBinding一样:
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:
我拖动A3到A1位置不成功,但B3将成功改变位置为B1A1.DisplayIndex!= B1.DisplayIndex?
为什么会这样?
答案 0 :(得分:0)
我用过
private static Dictionary<DataGridColumn, DataGridColumn> ColumnCache=new Dictionary<DataGridColumn,DataGridColumn>();
存储到DataGridColumn和
dgSource.ColumnDisplayIndexChanged += (sender1, e1) =>
{
var a=ColumnCache[e1.Column];
a.DisplayIndex = e1.Column.DisplayIndex;
};
更改columnDisplayIndex和我使用绑定时的错误:
Binding bindingDisplayIndex = new Binding();
bindingDisplayIndex.Mode = BindingMode.TwoWay;
bindingDisplayIndex.Source = A1;
bindingDisplayIndex.Path = new PropertyPath(DataGridColumn.DisplayIndexProperty);
BindingOperations.SetBinding(B1, DataGridColumn.DisplayIndexProperty, bindingDisplayIndex);
有决心。 也许在columnDisplay更改之前有一些自动句柄
答案 1 :(得分:0)
我花了一些时间才找到好方法,所以对于那些不想搜索的人:-) 谢谢你的帮助
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;
};
}