Best way to display multiple data items in WPF? - wpf

I know there has to be an easy way to do this, I just can't think of it. I need to display a set of notes in a WPF application. The layout for each note would be
Note Name Note Date
Note Text
Each item above exists individually in my database access layer and I just need a way to display it. In ASP, this is something I might have created a table for and then used the repeater object to dislay it multiple times (multiple notes).
Is the ListView control the best way to go about this? How would I display multiple items next to each other as above with the NoteName and NoteDate?

You could use a ListView. In this case you would declare a GridViewColumn for each column, with the appropriate DisplayMemberBinding or CellTemplate depending on the complexity of what you wanted to show.
You could also use an ItemsControl or a ListBox, with an appropriate DataTemplate. This is good for when you do not want a strict column layout (e.g. you want to show the Name and Date on one line of big bold text, and the Note Text below that in smaller italic text.)

Related

Change Columns based on DataType DataGrid WPF

I want to have different columns based on the datatype that is set on the DataGrid.
Example: if I supply ItemsSource of "core.Customer" or "core.ServiceLocation" I want different columns based on that datatype. How do? I constantly find conflicting information. I need guidance for this.
Edit: By the way I was hoping to define the columns via XAML, and load the "template" programmatically or define a "DataType" so it'd immediately know what column template to use.
Also, I have nested properties in my classes that i'd like to display.
Example: Customer class has "BillingName" property. I don't want to display "BillingName" I want to display "BillingName.First" and "BillingName.Last" instead. How to achieve this desired functionality?
After several hours of searching stackoverflow and google I found this which has my exact functionality desired under "Rendering templated columns"

I want a control for Hotel room booking status to show in wpf

I am planing to show hotel room booking status as a color effect. See the image. At want control I want to use?
Dude, this is WPF... you could use just about any control that you want to do that. Ok so I'm exaggerating a bit, but it really is your choice. You should use whatever you're comfortable with. However, if I had to design a UI that looked like your image, I'd probably either go with a ListBox or a DataGrid with a fairly high level of customisation.
If I were to use a DataGrid, the day names could be the column headers and the room names could be the row headers. You'd also need to use some sort of custom ColorConverter class to colour the cell data in the cells.
Using a ListBox, I'd create a DataTemplate to apply to the ListBox.ItemTemplate property that would contain the whole row including the room name. Of course, this would mean that you'd need to structure your data differently to include the name of each room. With a known number of days/columns, you could just use a Grid in the DataTemplate with the Grid.IsSharedSizeScope property set to True to keep the columns of each item aligned.
Good luck and have fun with that... it seems like an interesting project.

Can't set the table width in WPF through

I am making WPF application.I have a data grid and all columns have set their width to "Auto".When i start scrolling through the table some of the columns start expanding.My question is: Can I set the column width to fit the longest data in column at the beginning (without expanding columns when i scroll)?
the column width for wpf expanding when the shown data need more space when it set to Auto then this work will be done automatically.
Well this is due to the virtualization of the DataGrid. Only the items, which are visible are rendered and the controls are reused in case of scrolling (if you are activating it). And so the width will not be correct, since the longest element is not rendered yet. A list control performs very well, although you might bind many items to it.
I think you have 2 options
Turning off the virtualization, which might be a proper solution, if you don't have many items to show. I've to admit, I didn't try it, so no warranty. You can turn it off via <DataGrid VirtualizingStackPanel.IsVirtualizing="False"/>. For more information on the VirtualizationStackPanel pls have a look here.
Another solution may be TextTrimming. TextBlocks can show Ellipses if the text is too long. Therefor you will have to assign a custom datatemplate to the column put the following as content e.g. <TextBlock Text="{Binding}" TextTrimming="WordEllipsis"/>. Please note, that you will also have to provide a customer CellEditingTemplate, if the user shall be able to edit the values. For more information about TextTrimming please have a look here. To get an idea how the whole DataTemplate thing regarding DataGrids will work, you can have a look here.

WinForms - How do I create a table using a custom control for each row?

I want to create a table representing data I have, but I want each row to have a custom display. I have developed a little custom control that represents a row and has a few items in it (text box, check box, progress bar), however how do I now in the main form create multiple instances of this for the data I have?
e.g. is there a winforms control I can use to do this? or do I have to take a panel or something and programmatically do it?
I do need to somehow take responses back. So if someone clicks on the button in the 4th row say then I'll need to be able to tell which row it was from.
As an aside would then be a way to BIND the above mentioned visualization of my data to the data itself, say housed in an Array?
thanks
I see two options here:
You can use a DataRepeater. This control can be found in the Microsoft Visual Basic Powerpack. It allows you to place controls on a template which gets copied for each item in the databound collection.
You can create a custom control and manually place one instance of it for each item in a collection, re-creating databinding for the controls in it. This requires you to either expose the controls inside publicly or as properties of the user control.
However, above options are mostly useful for non-tabular data. If your layout is strictly tabular (i. e. rectangular cells in a grid) then you can create a custom DataGridViewCell which takes some time to understand but not too much code. Putting a progress bar into such a cell shouldn't prove too difficult.

How to display in each ListBoxItem textblocks with binding to a filelds of a different tables

Greetings to all and sorry for my English!
I have a ListBox, it's ItemsSource = myClientsList.DefaultView. The Items of ListBox have a template (ControlTemplate), that is defined in a in a separate resource file.
Every Item contains a little TextBlock's, Text -property of each have a binding to fields of my Object myClientsList.
I need to add in a this item template more TexBlock's and each of them must have binding to fields of another my class myOrdersList. - (So I wish to view on each line of ListBox information from different tables of my database - this is a question).
Problem in that that ListBox's ItemsSource have a link to object myClientsList and I cann't set myOrderList to ItemSource of same ListBox. So i must find a way to specify TextBlock.DataContext wich inside ControlTemplate or how it's possible to solve this problem in another way?
p.s. I'm a new in .Net and WPF and probably have a mistakes in my explanation - sorry for it.
It sounds like you have a DataGrid type of display and want to add more columns in order to display the order information for a given client. If this is the case, you are going to have to do a couple of things. First, you will need to create a composite object that stores information for both entities into a single object (so each row of your control has all the data it needs to display). Secondly, I would recommend using an actual DataGrid control to display rows instead of templating a ListBoxItem. The ListView with a GridView built into the framework isn't great, so I would recommend the WPFToolkit's DataGrid for a free option.
There are two issues here, if I've understood the question: how do you create a single collection containing both Clients and Orders, and how do you display Clients and Orders in different ways within the same ListBox?
Regarding the first, you can do this using a CompositeCollection.
Regarding the second, define two DataTemplates instead of a ControlTemplate. As the key of each DataTemplate, use the type of the object it is going to present e.g.
<DataTemplate x:Key="{x:Type local:Client}">
Alternatively, use ItemsControl.ItemTemplateSelector to explicitly point at different DataTemplates depending on the type of item. Ot if you really have to use ControlTemplates, check out ItemsControl.ItemContainerStyleSelector.

Resources