Hi,
I'm new to WPF and MVVM. I'm trying to keep my app as pure MVVM as possible.
I have a simple XamDataGrid with the fields defined externally. I want to control the visibility of some fields. I can easily change the FieldLayout property in the xaml.cs code, but that will not be MVVM. I tried to bind the FieldLayouts to my View Model, but that did not work.
So my question is - how can I modify the FieldLayout during runtime in an MVVM fashion? Binding the FieldLayout would solve my problem, but I could not figure out how to do that.
Thanks
Hi Caveno,
Perfect question - a great shame no one has answered you yet. I am experiencing exactly the same problem and have searched everywhere for a good solution.
I am also trying to implement a pure MVVM DataGrid solution - I've already done one the old fashioned way, hooking to events and using piles of code behind but that inevitably ends up in a mess.
What we really really really (!) want to do is something like: <XamDataGrid DataSource={Binding DataRows} FieldLayouts={Binding FieldDefinitions} /> (or similar). You can bind the DataSource, no problem, but the FieldLayouts and their Fields are not DP's. I appreciate the data source can be bound to any IEnumerable and that the FieldLayouts are more specific (in that they need to be bound to a collection of the correct type, so they can get the correct information about headers, bindings to the underlying data etc), but would have thought it should be possible to have a publically exposed "FieldLayoutViewModel" within the Infragistics API. I could then bind the FieldLayouts collection (in theory) to an ObservableCollection(of FieldLayoutViewModel) in order to manipulate the columns from within my main ViewModel - then it would feel like "proper MVVM".
I am continually faced with the fact that all examples I stumble across define the FieldLayouts up front in the XAML (a few try to dynamically generate them in the code behind). For simple scenarios and "fixed" applications, it's fine to setup the field layouts ahead of time. In our world (and I'm sure so many others must have this same scenario) we don't have a clue what columns to show on the grid ahead of time - we have an XML definition stored in a database that contains information about which columns should be available. The end user can even choose to change this definition at any point!
Even if I were willing to break the MVVM pattern and find another way, I can't find an obvious one. The XML definition exists in the grids ViewModel - but it's the View that needs to create the FieldLayouts (as they can't be bound). So, my View needs access to the definition as well. Ok, I can publically expose the definition within the ViewModel so I can bind to it from in the View. I bound the XamDataGrid's Tag property to the definition. The problem here is that the binding isn't resolved until the View is "Loaded" and the FieldLayoutInitializing event (where I want to generate the FieldLayouts) occurs prior to the View being loaded, so has no access to the definition :( Trying to pass the definition from the ViewModel to the View any earlier, or trying to get the FieldLayouts to re-initialize after everything has loaded, just feels so off track I’m sure it’s not the right way to go.
So, how do we dynamically define the columns from within the ViewModel?
Regards,
Dave