How to evenly space out GridViewColumns in GridView? - wpf

This is my XAML:
<ListView ItemsSource="{Binding Items}">
<ListView.View>
<GridView>
<GridViewColumn Header="Property1" DisplayMemberBinding="{Binding Property1}" />
<GridViewColumn Header="Property2" DisplayMemberBinding="{Binding Property2}" />
</GridView>
</ListView.View>
</ListView>
I want these 2 columns to take up the width of ListView in 1:1 ratio.
How can I achieve this?
Edit: I found this suggestion on MS Connect. This would perfectly solve my problem.
However it is closed as postponed (for 2.5 years now..)

What comes to mind is using internal Grids in the templates which each have a ColumnDefinition with the same SharedSizeGroup, the GridView should then have Grid.IsSharedSizeScope="True" and the columns of the grid view should themselves be unresizable (if that is possible), cannot test that right now so it's a bit sketchy.
Another method would be to bind both Widths of the GridViewColumns to the width of the ListView and then use a custom converter to get an appropriate fraction of that back.

Related

Limit the number of rows in a ListView / GridView

I have a ListView with a GridView used as the View, similar to the example here:
<Grid>
<ListView Margin="10" Name="lvUsers" ItemsSource="{Binding UsersView}">
<ListView.View>
<GridView>
<GridViewColumn Header="Name" Width="120" DisplayMemberBinding="{Binding Name}" />
<GridViewColumn Header="Age" Width="50" DisplayMemberBinding="{Binding Age}" />
</GridView>
</ListView.View>
</ListView>
</Grid>
How do I go about limiting the maximum number of rows displayed?
I've seen other examples refer to GridView.ItemsPanel (see below), but this simply does not work for me ("ItemsPanel not found in GridView").
Is my only solution to maintain the underlying CollectionView?
<GridView.ItemsPanel>
<ItemsPanelTemplate>
<ItemsWrapGrid MaximumRowsOrColumns="5"/>
</ItemsPanelTemplate>
</GridView.ItemsPanel>
Is my only solution to maintain the underlying CollectionView?
Yes. The control itself has no way of limiting the number of items.
You should control this yourself by not adding too many items to the source collection (UsersView in this case).
There is no way to limit the maximum number of items in XAML.
You could just filter your UsersView and use the Take method to get the number of items you want to show.
UsersView.Take(5);
Unless you need the whole UsersView in other places.

which wpf control should I use in case i need only 2 columns?

which wpf control Should i use in case I need only two columns. the first colum contains Property name and second column contains its value. however, on the top of all those properties I need a header of each columns like PROPERTY and VALUE. I know datagrid would not be a good solution for this. can someone suggest me which control to use or should I stick to Grid and set those header manually.
If you really can't use a DataGrid, then the GridView is probably your best bet... and when I say best, I mean easiest really. Assuming that you have a collection of your data type named Items and that the data type inside the collection has two properties, Property and Value, you could use this:
<ListView ItemsSource="{Binding Items}" HorizontalAlignment="Center"
VerticalAlignment="Center">
<ListView.View>
<GridView>
<GridViewColumn Header="Property" DisplayMemberBinding="{Binding Property}" />
<GridViewColumn Header="Value" DisplayMemberBinding="{Binding Value}"/>
</GridView>
</ListView.View>
</ListView>

Remove empty space/column on the right handside

in Wpf listview/gridview, how can I possibly remove empty space/column on the right handside ?
Anyone familiar with this annoying extra space ? I'd like to avoid setting fixed width, as I want my control to be fully sizeable.
Thanks for reading me ;)
<GroupBox>
<DockPanel>
<ListView>
<ListView.View>
<GridView>
<GridViewColumn Header="RIC" />
<GridViewColumn Header="Last tick" />
</GridView>
</ListView.View>
</ListView>
</DockPanel>
</GroupBox>
Thomas Levesque showed a link to the solution in comment :
The solution sits on that page WPF : Extend last column of ListView's GridView

Multicolumn listview in WPF at design time

I might sound dumb here, but I want to do something really simple. At design time, I want to add columns to a listview control and add some data to it. I need to add combobox in each column of the listview. The thing I am not able to find is where to mention the column number in the listviewitem. Any help appreciated guys.
<ListView IsSynchronizedWithCurrentItem="True" Margin="8,68,304,188"
BorderThickness="2,2,2,2">
<ListView.View>
<GridView>
<GridViewColumn Width="150" Header="Column1"/>
<GridViewColumn Width="150" Header="Column2"/>
</GridView>
</ListView.View>
<ListViewItem>
</ListViewItem>
</ListView>
Each column in the listviewitem is rendered based on the GridView definition, so there is no real concept of column numbers. What you do is bind objects to the the listview's itemsource and it creates listviewitems from it. Thus, there are a few hoops to jump through.
This link has an example of how to do some simple object data binding. The advantage of this is what binding structure you have for design time can probably be reused for run-time if you set the datacontext/itemsource to an empty object instead of the static one in XAML.
If you're doing this to show examples or you just have a static data source that you want to use, I would recommend using the XmlDataProvider. Then you'd change your ListView to be like this,
<ListView IsSynchronizedWithCurrentItem="True" Margin="8,68,304,188"
BorderThickness="2,2,2,2">
<ListView.View>
<GridView>
<GridViewColumn Width="150" Header="Column1" DisplayMemberPath="{Binding XPath=/A/B}"/>
<GridViewColumn Width="150" Header="Column2" DisplayMemberPath="{Binding XPath=/A/C"/>
</GridView>
</ListView.View>
<ListViewItem>
</ListViewItem>
</ListView>

WPF listview remove extra column generated

When I use a ListView in WPF it always generates one extra column at the end of the ListView. For example, if I define two columns in my listview and when I run it it generates those two columns plus one empty column header. Any idea how I can remove that?
Sample ListView XAML
<ListView ItemsSource="{Binding Path=SearchAttributes}"
DockPanel.Dock="Top">
<ListView.View>
<GridView x:Name="grdView">
<GridViewColumn Header="Name" DisplayMemberBinding="{Binding SearchFieldName}" />
<GridViewColumn Header="Balance" Width="Auto"
CellTemplateSelector="{StaticResource searchFilterDataTemplateSelector}"
>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
Thanks,
Jithu
The last column is simply the left over space you need to size the columns to fit exactly in the space. By defining the width as Auto you are making sure that is only as big as possible.
There should be a property in GridView, something like AutoGenerateColumns - set it to "False"

Resources