Is there a way to produce a scrollable gridlayout with dynamic content? - c#

Ì want to write a software where there is a grid layout inside a scrollview.
I already hardcoded it, but I think I need to find a solution to make this dynamic! How can I manage to do this! I am pretty new to C# and WPF. I post a screenshot and my code so you can see what I am trying to achieve.

DataGrid is probably the best thing to use here, although you can also do it with a ListView using a GridView as the view:
<ListView ItemsSource="{Binding Items}">
<ListView.View>
<GridView>
<GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}" />
<GridViewColumn Header="Age" DisplayMemberBinding="{Binding Age}" />
<GridViewColumn Header="Gender" DisplayMemberBinding="{Binding Gender}" />
</GridView>
</ListView.View>
</ListView>

Related

wpf width issues with listview in listviewitem

I have a listview in which I have several listviewitems but I want to show additional information, therefore I added a expander which shows another listview within said listviewitem. But my problem is that the listview is only as wide as the listviews column width.
XAML
<ListView.View>
<GridView>
<GridViewColumn>
<GridViewColumn.CellTemplate>
<DataTemplate>
<Expander>
<ListView>
<ListView.View>
<GridView>
<GridViewColumn Header="Order number" DisplayMemberBinding="{Binding Id}"/>
<GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}"/>
<GridViewColumn Header="Price" DisplayMemberBinding="{Binding Price}"/>
</GridView>
</ListView.View>
</ListView>
</Expander>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Order number" DisplayMemberBinding="{Binding OrderNumber}"/>
<GridViewColumn Header="Customer" DisplayMemberBinding="{Binding Customer}"/>
<GridViewColumn Header="Total Price" DisplayMemberBinding="{Binding CombinedPrice}"/>
<GridViewColumn Header="Urgency" DisplayMemberBinding="{Binding Urgency}"/>
</GridView>
</ListView.View>
</ListView>
Your issue is because the expander is within the cell in the listview and that cell will constrain it's size.
It will not expand to the size of it's content because ListView columns do not dynamically resize.
You could consider using a togglebutton and popup instead. Bind the isopen of the popup to the togglebutton ischecked.
A popup is a separate window effectively and hence not inside that "box" around the cell.
You could alternatively consider a datagrid which has rowdetails expand out below a row.
https://www.wpf-tutorial.com/datagrid-control/details-row/

How to Bind Values from List in BarChart

I have an application which is check the incoming Mails on the Server and refresh a List with X-Values in it. Now I want to Plot the incoming Mail count in a Live Chart. I use http://lvcharts.net for this.
I have two classes a Mail Object with the Count and a TimeStamp in it and a List Class with all Mail Objects in it.
Here is my sample(pseudo) XAMl code:
<liveCharts:BarChart Name="MailChart" Grid.Row="1" Grid.Column="0">
<liveCharts:BarChart.Series>
<liveCharts:BarSeries Title="Mails" Values= "{Binding MailCountList.Count}"></liveCharts:BarSeries>
......
......
<liveCharts:BarChart.AxisY>
<liveCharts:Axis Title="Time" Labels="{Binding MailCountList.Timestamp}">
......
The Objects are in a normal List with automatically Refesh I use MVVM.
My Question is:
How can I bind a List to a XAML Value Series ?
I have used an ObservableCollection to view the Content and a normal list to fill sort etc. the Xaml Code Looks Like:
<ListView ItemsSource="{Binding TableList}" Grid.Row="0">
<ListView.View>
<GridView>
<GridViewColumn Width="50" Header="ID" DisplayMemberBinding="{Binding ID}"/>
<GridViewColumn Width="180" Header="Name" DisplayMemberBinding="{Binding Name}"/>
<GridViewColumn Width="100" Header="Group" DisplayMemberBinding="{Binding Group}"/>
<GridViewColumn Width="85" Header="Version" DisplayMemberBinding="{Binding Version}"/>
</GridView>
</ListView.View>
</ListView>
The Source of the ListView is in the ItemSource and to Display Multiple Rows I use GridViewColumn with Binding to the Object Values from the List in "DisplayMemberBinding"

WPF ListView very poor performance with large data

I am experiencing very poor performance with a ListView in WPF, using around 30000 records. As far as I know Virtualisation should be turned on as this is the default (I even turned it on explicitly in the XAML).
The poor performance manifests in this way:
Very slow (a couple of minutes) to do the initial bind
Very slow (over a minute) scrolling
Very slow (again, well over a minute) when you select a row.
I was hoping someone would take a look at the XAML and let me have some thoughts.
<ListView Name="grdComms" Grid.Row="0" Grid.Column="0" SelectedItem="{Binding SelectedHeader}"
VirtualizingStackPanel.IsVirtualizing="True" VirtualizingStackPanel.VirtualizationMode="Recycling"
ScrollViewer.IsDeferredScrollingEnabled="True">
<ListView.View>
<GridView >
<GridViewColumn Header="Account Name" DisplayMemberBinding="{Binding Path=AccountName}" Width="150" />
<GridViewColumn Header="Account Number" DisplayMemberBinding="{Binding Path=AccountNumber}" Width="120" />
<GridViewColumn Header="Type" DisplayMemberBinding="{Binding Path=Type}" Width="80" />
<GridViewColumn Header="Delivery" DisplayMemberBinding="{Binding Path=Delivery}" Width="80" />
<GridViewColumn Header="Count" DisplayMemberBinding="{Binding Path=RequestCount}" Width="80" />
<GridViewColumn Width="80" Header="DeDupe">
<GridViewColumn.CellTemplate>
<DataTemplate>
<StackPanel Width="80">
<CheckBox HorizontalAlignment="Center" IsChecked="{Binding Path=SelectedForProcessing, Mode=TwoWay}"/>
</StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
Note: the ItemsSource is set in code, to an ObservableCollection. This is a collection of pretty plain properties (couple of strings, a bool), which is a ViewModel onto the Model, which is (again) strings and bools.
I'm reading where people are using large record sets with no problems, but the various things I have tried don't seem to work.
Any more information required please let me know.
Please ignore me. The problem entirely disappears as soon as I set the MaxHeight of the ListView to something bigger than it needs. I would swear blind I tried this, obviously not.
Move along, there is nothing to see here ...
Gray

How to customize WPF TreeView columns individually?

I have the Xaml code that show everything correctly but I want to make it so the first column's content is bold:
<c:TreeView Name="JobList" SelectedItemChanged="JobList_SelectedItemChanged">
<c:TreeView.Columns>
<GridViewColumn Header="Jobs" Width="350" DisplayMemberBinding="{Binding Name}"/>
<GridViewColumn Header="Goal" Width="100" DisplayMemberBinding="{Binding Goal}"/>
<GridViewColumn Header="Messages" Width="120" DisplayMemberBinding="{Binding MessageType}"/>
</c:TreeView.Columns>
</c:TreeView>
Also can I also do this based on whether the item in the first column has sub treeview nodes or not?
I havent used the TreeListView, but usually you can put other UIElements in the header. Give this a try:
<GridViewColumn Header="Jobs" Width="350" DisplayMemberBinding="{Binding Name}">
<GridViewColumn.Header>
<TextBlock FontWeight="Bold" Text="{Binding Goal}"></TextBlock>
</GridViewColumn.Header>
</GridViewColumn>

ListView that imitates Windows Explorer

How can I make a ListView imitate the ListView in Windows Explorer on the right side. Like how can I get icons in the ListView and get the arrows?
You can find the icons by using Google Image search. To create the ListView, you could do something like this is XAML:
<Grid>
<ListView ItemsSource="{Binding ListViewSource}">
<ListView.View>
<GridView>
<GridViewColumn Width="25">
<GridViewColumn.CellTemplate>
<DataTemplate>
<Image Source="{Binding Icon}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Name" DisplayMemberBinding="{Binding FileName}" Width="250"/>
<GridViewColumn Header="Date Modified" DisplayMemberBinding="{Binding DateModified}" Width="100"/>
<GridViewColumn Header="Type" DisplayMemberBinding="{Binding FileType}" Width="100"/>
<GridViewColumn Header="Size" DisplayMemberBinding="{Binding FileSize}" Width="100"/>
</GridView>
</ListView.View>
</ListView>
</Grid>
The next step is to create an ObservableCollection to hold all of the items in your list and call it ListViewSource. You can populate this collection with actual FileDirectory information, or your own kind of list. You'll then want to create your logic as to what happens when you doubleclick on an item. Since your question didn't specify to what detail you want the ListView to work, I'm going to stop there. Let us know if you want it to behave just like Windows Explorer, and we'll try to help you out.

Categories