I have a wpf datagrid (.NET 4.0) that contains raw data from a database, and some of the fields need to be formatted to display in a meaningful manner, for example 3 fields containing Year / Month / Day could be formatted together to produce a properly formatted date field.
Is there a nice way of doing this ?
Separate your View/XAML from your ViewModel, and keep your formatting in the ViewModel as this doesnt belong in the View!
then you can bind your date field to your datagrid and not mess up your XAML or code-behind.
If you look for a good framework to help you keep this design I recommend looking at MVVM Light or Caliburn.Micro
You scould not bInd directly you data, but you have to create a function on your model and then bind the function on you view
Related
I have an object which has some 30 properties, depending on who is viewing the data, I need to present just 10-12 properties to him.
These properties will form the columns of the WPF datagrid. I am using .Net 4.0
However, all this will happen at run time. I need to do this is the MVVM way.
Any direction as to how to achieve this will be appreciated.
regards,
You have two options as far as I can see.
1) Expose a ViewableDetailsType enum property from your ViewModel that tell you what kind of view of the data you should show. In the view you can then create triggers in your DataGrid to set the Columns property of the grid to manually show the appropriate columns.
2) A better way would be to create wrapper, DTO type ViewModel objects for your underlying model object. One for each view of the object you want to expose. You then expose a collection of the appropriate wrapper objects to the view, and the DataGrid can use auto columns.
This is slightly more work, but it is truer to MVVM since the data hiding is taking place in the ViewModel, and can thus be tested.
I am filling up a Stackpanel with Textboxes. Each textbox, Should display the Value Property of a Class i have written.
The number of Textboxes is always different so i can't create a databinding in the xaml file.
Are there tutorials, that show how can i archive my goal?
Edit: Thanks for the replies so far. After reading the articles i still not clear how to implement my Problem.
Here is what I'm trying to achieve:
I have an SQL Server DB and there a Table with an ID and a Value.
I now load all Values and IDs i want to display into my application. (number of loaded rows is always differnt).
I need to display a textbox for each row and display the value there and after the value is changed i write the value back to the database.
I don't know how I should query the data and than bind it to the textboxes.
I don't need an implementations, I am happy with every piece of advice I can get.
yes there are a number of tutorials out there. It sound like you are interested in datatemplating.
I would suggest looking Here on MSDN. A few tutorials on databinding in general may be useful DataBinding on MSDN
If your class, classValue, has a public property Value then just create a List and bind that to a ListBox with the item DisplayValuePath = Value. Repeater controls are used to bind to collections.
What is the best practice to use data sets instead of an observable collection in MVVM to bind to grids. is it OK to have a property of type DataSet on the view model? How is the design time data set in this case?
One of the key concepts of the MVVM pattern is that the ViewModel is the "model of your view", it shapes your business model in such a way that it is easier to bind a UI (i.e. your View) to it.
You can certainly use a DataSet / DataTable to expose data from your ViewModel and bind it to your View. I don't see anything wrong with this approach. It is certainly valid MVVM!
Regarding design time data, it depends on how you are creating it. You cannot create a DataSet in XAML, so cannot use a XAML file within visual studio for your data. However, if you are programmatically creating design time data, i.e. within your ViewModel checking whether it is design-time, then creating data in your code, it will work just fine.
I need to get all the cells within a certain column.
there is no property called "Cells" in the GridViewColumn class.
Is there any other way of doing this?
Thanks.
May I ask you the purpose of getting those cells? And when you say you want a cell, do you want the visual of that cell or the data associated with it?
If it is data, then getting it directly from your ViewModel will be a preferred approach. So if you are following proper MVVM while implementing this, you can have a method in the ViewModel which can easily expose you a specific collection of cell-data corresponds to a column
Have a look at http://techiethings.blogspot.com/2010/05/get-wpf-datagrid-row-and-cell.html
I need to create a table structure that will automatically create columns for a collection of objects (the structure of which is pretty flexible). The data in the table will only be read only so I've been looking into using a GridView but can't figure out how to automatically generate the columns - has anyone got an example or a URL explaining how to do it?
I'm not totally adverse to using the DataGrid control included as part of the WPF ToolKit, but it seems a little over-kill for displaying readonly data...
I would look at this post on generating the columns for a GridView:
WPF GridView with a dynamic definition
Using the DataGrid would definitely be overkill.