コード例 #1
0
 private void OnSettingsChanged(DataGrid dataGrid, IFieldMapperGridViewModel fmgvm)
 {
     if (_updatePending)
     {
         return;
     }
     _updatePending = true;
     BackgroundTaskManager.DelayedPostIfPossible(
         () =>
     {
         _updatePending = false;
         foreach (var column in dataGrid.Columns)
         {
             var matchingViewModelColumn = fmgvm.Columns.Where(col => col.Header == column.SortMemberPath).FirstOrDefault();
             if (matchingViewModelColumn != null)
             {
                 var pfd           = new PrivateFieldData();
                 pfd.DisplayIndex  = column.DisplayIndex;
                 pfd.SortDirection = column.SortDirection;
                 pfd.Visibility    = column.Visibility;
                 matchingViewModelColumn.InternalProperties = pfd;
             }
         }
         fmgvm.RaiseColumnSettingsChanged();
         return(true);
     });
 }
コード例 #2
0
        private void RebuildDataGridColumns(IFieldMapperGridViewModel fmgvm, DataGrid dataGrid, PropertyDescriptor pd, FieldMapperGridControl host, bool updateFromInternalData = false)
        {
            int idx = 0;

            dataGrid.Columns.Clear();
            foreach (var column in fmgvm.Columns)
            {
                DataGridColumn dataGridColumn = null;
                if (host.ItemTemplates.ContainsKey(column.Column))
                {
                    dataGridColumn = new DataGridTemplateColumn()
                    {
                        CellTemplate = host.ItemTemplates[column.Column]
                    };
                    dataGridColumn.SortMemberPath = column.Column;
                }
                else if (host.ItemTemplates.ContainsKey(String.Empty))
                {
                    dataGridColumn = new DataGridTemplateColumn()
                    {
                        CellTemplate = host.ItemTemplates[String.Empty]
                    };
                    dataGridColumn.SortMemberPath = column.Column;
                }
                else if (host.ItemTemplates.ContainsKey("Type:" + column.Type.FullName))
                {
                    dataGridColumn = new CustomBoundColumn()
                    {
                        ContentTemplate = host.ItemTemplates["Type:" + column.Type.FullName], Binding = new Binding("Item[" + column.Column + "]")
                    };
                    dataGridColumn.SortMemberPath = column.Column;
                }
                else
                {
                    dataGridColumn = new DataGridTextColumn()
                    {
                        Binding = new Binding("Item[" + column.Column + "]")
                    };
                    if (column.Type == typeof(DateTime))
                    {
                        (dataGridColumn as DataGridTextColumn).Binding.StringFormat = CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern + " " + CultureInfo.CurrentCulture.DateTimeFormat.LongTimePattern;
                    }
                    dataGridColumn.SortMemberPath = column.Column;
                }

                dataGridColumn.Header     = column.Header;
                dataGridColumn.Visibility = column.Visible ? Visibility.Visible : Visibility.Collapsed;
                dataGridColumn.Width      = new DataGridLength(column.Width);

                pd.AddValueChanged(dataGridColumn, (ox, ax) =>
                {
                    column.SuppressNotifications();
                    column.Width = dataGridColumn.Width.IsAuto ? dataGridColumn.Width.DisplayValue : dataGridColumn.Width.Value;
                    column.AllowNotifications();
                });
                column.PropertyChanged += (ox, ax) =>
                {
                    if (ax.PropertyName == "Visible")
                    {
                        if (column.Visible == false)
                        {
                            BackgroundTaskManager.DelayedPostIfPossible(() => { RebuildDataGridColumns(fmgvm, dataGrid, pd, host, false); return(true); });
                        }
                    }
                    if (ax.PropertyName == "Width")
                    {
                        dataGridColumn.Width = new DataGridLength(column.Width);
                    }
                };
                BindingOperations.SetBinding(dataGridColumn, DataGridTemplateColumn.WidthProperty, new Binding()
                {
                    Source = column.Width
                });
                dataGrid.Columns.Add(dataGridColumn);
                ++idx;
            }

            dataGrid.ColumnDisplayIndexChanged += (o, a) => OnSettingsChanged(dataGrid, fmgvm);
            dataGrid.ColumnReordered           += (o, a) => OnSettingsChanged(dataGrid, fmgvm);
            dataGrid.Sorting += (o, a) => OnSettingsChanged(dataGrid, fmgvm);
            dataGrid.Items.SortDescriptions.Clear();

            foreach (var column in fmgvm.Columns)
            {
                var dataGridColumn = dataGrid.Columns.Where(item => item.SortMemberPath == column.Header).FirstOrDefault();
                if (dataGridColumn == null)
                {
                    continue;
                }

                var fromInternalData = column.InternalProperties as PrivateFieldData?;
                if (fromInternalData != null)
                {
                    try
                    {
                        dataGridColumn.SortDirection = fromInternalData.Value.SortDirection;
                        if (dataGridColumn.SortDirection.HasValue)
                        {
                            dataGrid.Items.SortDescriptions.Add(new SortDescription(dataGridColumn.SortMemberPath, dataGridColumn.SortDirection.Value));
                        }
                        if (updateFromInternalData)
                        {
                            dataGridColumn.Visibility = fromInternalData.Value.Visibility;
                        }
                    }
                    catch (Exception)
                    {
                    }
                }
            }
        }