I am using 14.2 for my wpf project. I want to have a window with a xamDataGrid in it and have that window reusable. I have 11 models. The window will show a given models task(which is data that needs maintenance). A model may have between 2 to 7 different data sources(derived from Linq to EntityFramework). The user will choose from a dropdown which table/task to edit in the grid.
Consistent behavior should be as follows:
So I understand I can create field layouts in XAML, however I am wondering if I can do the same thing in the code behind and have the behavior that is required?
Another question is; how do I toggle between the different layouts when I change the data source?
Hello:
I also have this issue, wherein I have several different slices of the data in different models and must display them in completely different column layouts.
My intention was to create several views one for each data model and simply make the inactive ones collapsed or hidden depending on say a DataTrigger. I'm very interested in what will be your ultimate solution.
Hi jerovsek,
An approach you can take for this would be to allow the XamDataGrid to automatically generate the FieldLayout for you. If AutoGenerateFields is set to true, it will look at your data and automatically create a FieldLayout for it. You can then handle the FieldLayoutInitialized event and use this to customize your Fields further. This would be the place to set whether a Field is editable or not, or whether it should be visible or not. In the Field.Settings object there is an AllowEdit property which you can set to enable/disable editing on the entire column. Controlling the Visibility is as simple as setting the Field.Visibility property.
Changing the background of a cell based on whether it is editable or not will require a new style for the CellValuePresenter control. You can create a DataTrigger in the Style.Triggers collection and bind the trigger to the CellValuePresenter.IsEditingAllowed property and change the background based on this.
<Style x:Key="NonEditableCellStyle" TargetType="{x:Type igDP:CellValuePresenter}"> <Style.Triggers> <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsEditingAllowed}" Value="False"> <Setter Property="Background" Value="Red"/> </DataTrigger> </Style.Triggers> </Style>
Using this style is as simple as setting the Field.Settings.CellValuePresenterStyle property:
Field myNonEditableField = e.FieldLayout.Fields["MyFieldName"]; myNonEditableField.Settings.AllowEdit = false; myNonEditableField.Settings.CellValuePresenterStyle = this.Resources["NonEditableCellStyle"] as Style;
With auto-generated field layouts, you don't have to worry about switching between layouts when the data source changes. When the data source changes, the XamDataGrid will look to see if it has any matching FieldLayouts for the properties inside the data source. If it doesn't find a complete match it will generate a field layout for you, at which point the FieldLayoutInitialized event will fire.
I hope this makes sense. Let me know if you have any questions.