Handling the ColectionChanged event allows to handle the TextBox input. The column's TextBox will bind to a ObservableCollection of string values (filter expressions), where the index of each item maps directly to a column index. This will be more convenient as the TextBox will now automatically resize and move (in case the column is dragged). Since the TextBox elements are meant to filter based on their associated column, I would suggest to modify the DataGrid.ColumnHeaderStyle to add a TextBox to the column header. I have added an extension method that converts a collection to a DataTable, in case you need it. To dynamically change column count, you should always use a DataTable as data source. In both cases you should implement an IValueConverter to convert from double to Thickness (for the Margin) and bind the StackPanel.Margin of the first example or the ItemsControl.Margin of the second example to the DataGrid.RowHeaderActualWidth property to adjust for the row header (to align the text boxes properly).īecause you have provided more information, I felt the need to either delete or adjust my answer: To make a TextBox to follow the width of its respective column, simply use data binding: Īnd to make it dynamic, simply use an ItemsControl that is configured to display its items horizontally: Code will always become smelly and overly complex when avoiding data binding and XAML. The examples also highlight the efficiency of data binding in WPF. It will only make your UI slow.īelow you find two examples: a static, hard-coded version and a more elegant and dynamic version (where text boxes are added automatically to match the column count).īoth examples will automatically adjust the width of each TextBox if columns are resized (as a bonus when using data binding). It's also very likely that calling UpdateLayout() and Items.Refresh() should be avoided to improve the performance.Ĭhanging the data source ( ObservableCollection) will already trigger a refresh and, if necessary, a complete layout pass. Your Thread.Sleep looks very very suspicious too. Avoiding XAML where XAML is possible will make life a lot harder most of the time and your code will start to smell. Using data binding should be the most intuitive. Is there any method to wait the Observablecollection to update properly? PrintTextBlock.Name = dc.Header.ToString().Replace("_", "_") PrintTextBlock.Margin = new Thickness(0.5, 0, 0, 0) Public void AddRegexItemToStackpanel(string parameterName, DataGridColumn dc) When I debug the whole process it runs slower and I get a result like this (Which I want to get without debug mode): When running the code in "normal" speed I get a result like this: It is my goal to create a Stackpanel with Textboxes, which do have the same height as the actualized Columnwidths, which are set to Auto. My ObservableCollection contains of some Elements which are created when doing a selection in Revit. (I´m creating a little plugin for Revit). I´ve got a DataGrid whose ItemSource is fed by an ObservableCollection. I´m kind of stuck here with a little problem.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |