Remove empty space/column on the right handside - wpf

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

Related

Disable resizing of a column in a ListView

There's a ListView in my window that has a number of columns. Some or all of the columns must not be resizable by the user because they already have the optimal width and accidental resizing only makes it worse. Now there's some answers to that question available, but all of them end up with restyling the control. Unfortunately then I end up with numerous pages of XAML code which is highly platform/theme-specific. When I create a copy of the default style with Blend, I get lots of gradients etc that only work on Win7 Aero, but not in XP theme or whatever will come.
So replacing the entire style of a control is not an option. (It hardly ever really is.)
I've already identified the part that needs to be hidden, it's named "PART_HeaderGripper". I've done such things before, removing the running glow and other parts from a ProgressBar with the following code in code-behind:
var glow = progressBar.Template.FindName("PART_GlowRect", progressBar) as FrameworkElement;
if (glow != null) glow.Visibility = visibility;
But this doesn't work with a GridViewColumnHeader because Template.FindName doesn't find anything (returns null). I'm pretty sure there must be a way to modify the visuals at runtime. But I can't figure it out right now. Any idea?
Tested:
<GridViewColumnHeader Content="Value" IsHitTestVisible="False"/>
I had the same issue in one of my projects. I have found a solution, which I think not the best way, but at least works.
I am using some databingind and click events to sort the items, so setting the column to readonly was not a way to go.
Here is the code part from the xml:
<ListView Name="ListWievResults" HorizontalAlignment="Left" Height="526" Margin="10,10,0,0" VerticalAlignment="Top" Width="400" ItemsSource="{Binding Results}" SelectedItem="{Binding SelectedResult}" IsSynchronizedWithCurrentItem="True" SelectionChanged="ListWievResults_SelectionChanged" SelectionMode="Single">
<ListView.View>
<GridView AllowsColumnReorder="False">
<GridView.Columns>
<GridViewColumn DisplayMemberBinding="{Binding Path=Name}" Width="200">
<GridViewColumnHeader Content="Parameter" Click="GridViewColumnHeader_Click" PreviewMouseMove="GridViewColumnHeader_PreviewMouseMove"/>
</GridViewColumn>
<GridViewColumn DisplayMemberBinding="{Binding Path=ID}" Width="100">
<GridViewColumnHeader Content="SAFE ID" Click="GridViewColumnHeader_Click" PreviewMouseMove="GridViewColumnHeader_PreviewMouseMove"/>
</GridViewColumn>
<GridViewColumn DisplayMemberBinding="{Binding Path=Author}" Width="95">
<GridViewColumnHeader Content="Author" Click="GridViewColumnHeader_Click" PreviewMouseMove="GridViewColumnHeader_PreviewMouseMove"/>
</GridViewColumn>
</GridView.Columns>
</GridView>
</ListView.View>
</ListView>
and the related part from th xaml.cs file:
private void GridViewColumnHeader_PreviewMouseMove(object sender, System.Windows.Input.MouseEventArgs e)
{
e.Handled = true;
}
This is not the most beautiful way as the marker will still be there showing that you can resize the column, but at least it does not allow it.
One drawback is that if you want to use the "PreviewMouseMove" event for something else, that will not work.
I really hope that this can help.

How to evenly space out GridViewColumns in GridView?

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.

How to set property only on second column of a ListView?

Introduction
I have a ListView and want to format only the second column. The following XAML code does that:
<ListView x:Name="listview">
<ListView.View>
<GridView>
<GridViewColumn Header="Property" DisplayMemberBinding="{Binding Path=Key}" Width="100"/>
<!-- <GridViewColumn Header="Value" DisplayMemberBinding="{Binding Path=Value}" Width="250">-->
<GridViewColumn Header="Value" Width="250">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Value}" Foreground="CornflowerBlue" AutomationProperties.Name="{Binding Path=Key}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
The one problem I have is that the AutomationProperties.Name property is not being set. I was checking it with the Coded UI Test Builder and the property is empty. The Text and the Foreground property are being set correctly.
Question
Does anyone know why AutomationProperties.Name is not being set?
Additional information
Strangly enough, the following XAML code does set the AutomationProperties.Name
<ListView x:Name="listview">
<ListView.Resources>
<Style TargetType="TextBlock">
<Setter Property="AutomationProperties.Name" Value="{Binding Key}"/>
</Style>
</ListView.Resources>
<ListView.View>
<GridView>
<GridViewColumn Header="Property" DisplayMemberBinding="{Binding Path=Key}" Width="100"/>
<GridViewColumn Header="Value" DisplayMemberBinding="{Binding Path=Value}" Width="250"/>
</GridView>
</ListView.View>
</ListView>
The problem here though is that AutomationProperties.Name is being set on all the columns. But I only want it on the second one because otherwise my Coded UI Test code returns the wrong value (that of the first column, instead of that of the second column which I want).
Don't know if you're aware of this, but a very helpful tool in analyzing these types of problems is Snoop.
In particular, it will highlight (with red) any data binding errors you may have.
I took a look myself and it sure seems as if the first piece of xaml (above) is now working (after you cleared up the syntax error). In Snoop, bound properties are highlighted with a light green.
Here is a screen shot of Snoop showing the property is being set correctly:
And here is a screen shot of Snoop showing the TextBlock (where the property isn't set ... no light green column) on the first column:
And, finally, I've intentionally broken the binding to show you what Snoop shows when something is wrong and you have a data binding error (it is highlighted in red and one of the columns gives you additional information):

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