Make a multi column button with a Gridview - wpf

I have the following XAML. I want one button per row to encompass all the data. Is this possible?
<ListView Name="ClosestListView">
<ListView.View>
<GridView>
<GridView.Columns>
<GridViewColumn Header="ShapeFileType" >
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=ShapeFileType}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Address" >
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=fullMatch}" MinWidth="100"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
... more columns and closing tags

If you're just looking to add a Click event to your Row, you would probably be better off adding a Click event to your ListViewItem or GridViewCell
<Style TargetType="{x:Type ListViewItem}">
<EventSetter Event="MouseDoubleClick" Handler="HandleDoubleClick" />
</Style>
If you want to bind the click event to a Command in your ViewModel, I'd suggest using the AttachedCommandBehavior found here
And if you really want a button that spams all columns, I would recommend looking into Grid.SharedSizeGroup to align your data

I guess its more logical to just handle selectionchanged event.
<ListView Name="ClosestListView" SelectionChanged="ClosestListView_SelectionChanged">

Related

Disable ListView selection and hover

Usually I can disable ListView selection doing like this thread suggests - or something similar where you set the ItemContainerStyle.
However I have this ListView that is defined like this:
<ScrollViewer>
<ListView ItemsSource="{Binding List, Mode=OneWay}">
<ListView.View>
<GridView>
<GridViewColumn>
<GridViewColumnHeader Style="{StaticResource header}"/>
<GridViewColumn.CellTemplate>
<DataTemplate>
<!-- Some data -->
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridViewColumn>
...
</GridView>
</ListView.View>
</ListView>
</ScrollViewer>
And if I try to specify the ItemContainerStyle - the data in the list just disappears.
Giving the above ListView, how would I proceed just to removed the selection?
Can you try this ItemContainerStyle? This basically will not pick up any input events.
<ListView ItemsSource="{Binding List, Mode=OneWay}">
<ListView.ItemContainerStyle>
<Style TargetType="ListBoxItem" BasedOn="{StaticResource {x:Type ListViewItem}}">
<Setter Property="IsHitTestVisible" Value="False"/>
</Style>
</ListView.ItemContainerStyle>
</ListView>

Why doesn't my text block style get applied?

I have a style created in my windows resources area:
<Style TargetType="TextBlock">
<Setter Property="TextTrimming" Value="CharacterEllipsis"/>
</Style>
I thought this meant that all text blocks would have this style, so when I create my list view column:
<GridViewColumn>
<GridViewColumnHeader Content="Source"/>
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Source, Mode=OneWay}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
The ellipsis style is NOT applied to the text block in the column.
If I name the style with x:Key, then use Style={StaticResource xxx} then it works - why doesn't the unnamed approach work?
Here's the complete window XAML:
<Window x:Class="ListViewStyle.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<Style TargetType="TextBlock">
<Setter Property="TextTrimming" Value="CharacterEllipsis"/>
</Style>
</Window.Resources>
<ListView ItemsSource="{Binding Rows}">
<ListView.View>
<GridView>
<GridViewColumn>
<GridViewColumnHeader Content="Source"/>
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Source, Mode=OneWay}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn>
<GridViewColumnHeader Content="Primary"/>
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Primary, Mode=OneWay}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn>
<GridViewColumnHeader Content="Secondary"/>
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Secondary, Mode=OneWay}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
</Window>
You may be defining another TextBlock style, closer to the TextBlock itself.
The way WPF searches for a style starts with the item to be styled. If it sets the Style property, then it uses that. Otherwise, it checks the parent's resources, then its grandparent's resources, and so on, using the first style it finds. If it doesn't find a style in your application it uses the default style.
There is probably a TextBlock style in one of the TextBlock's ancestors. If it finds a style there, it won't use the one at the window level.
You can specify that one style is based on another, with the BasedOn property.

C# WPF Dynamic UniformGrid

I have a textbox where you can input an integer (which will the size of the UniformGrid). On a button click, a UniformGrid is generated with each grid contains some textblock, textbox, and a button, generated under different ElementName. I went through so many tutorial and all simply Add something to the children. And I can't seem to set the binding logic worked out - which bind to what, and which is being itemcontrolled. I went through wpftutorial and it just confuses me further. Appreciate if anyone can explain the logic in simple terms.
UniformGrid cannot really be used as an ItemsHost for an ItemsControl, mainly because the DataTemplate can only take a single child, which prohibits its use in this context.
Here's an alternative approach that might help you achieve what you want (I think)
<ListView ItemsSource="{Binding MyData}">
<ListView.View>
<GridView>
<GridView.ColumnHeaderContainerStyle>
<Style TargetType="GridViewColumnHeader">
<Setter Property="Visibility" Value="Collapsed" />
</Style>
</GridView.ColumnHeaderContainerStyle>
<GridView.Columns>
<GridViewColumn>
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding SomeLabelText}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn>
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBox Text="{Binding SomeInputText}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn>
<GridViewColumn.CellTemplate>
<DataTemplate>
<Button Content="{Binding SomeButtonLabel}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView.Columns>
</GridView>
</ListView.View>

How to have image and button column in ListView in WPF

I have to add two columns in a ListView. One column shows different Images and another column is used to show a Button.
It will be great help if anybody provide the any link or example source code.
Thanks in advance.
This would give you two columns one with a button one with an image. if i was doing it i would put a command on my item so i could bind my button to it.
<ListView
Name="myListView">
<ListView.View>
<GridView>
<GridViewColumn>
<GridViewColumn.CellTemplate>
<DataTemplate>
<Button
Content="Click Me"
Margin="0"
VerticalAlignment="Center"
Click="Button_Click" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn>
<GridViewColumn.CellTemplate>
<DataTemplate>
<Image
Source="{Binding ImageSource}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
You would need to have an items source containing items that have a property called ImageSource

Make WPF ComboBoxes fill a whole column width

I'm having problems making a ComboBox stretch to fill the whole column width in a GridViewColumn. It should also resize when the column is resized.
In the following example I have a StackPanel with a ComboBox inside. This is set to stretch and will in fact stretch to fill the StackPanel width.
Then I add a ListView with one column, containing a StackPanel with a ComboBox. Both the StackPanel and the ComboBox are set to stretch, but they don't. I use background colors to identify the size of the StackPanels, and there is no red unless I set a width or add elements to the ComboBox such that it needs more width.
I also tried playing around with the HorizontalContentAlignment property without success.
<StackPanel Height="59" Margin="45,12,38,0" VerticalAlignment="Top" Background="Green">
<ComboBox HorizontalAlignment="Stretch" />
</StackPanel>
<ListView x:Name="MyListView" Margin="0,106,0,0">
<ListView.View>
<GridView>
<GridViewColumn Header="Num" Width="70">
<GridViewColumn.CellTemplate>
<DataTemplate>
<StackPanel Background="red" Orientation="Horizontal" HorizontalAlignment="Stretch">
<ComboBox HorizontalAlignment="Stretch" />
</StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
<ListViewItem></ListViewItem>
</ListView>
Try setting the Style of the ListViewItem. I also removed your StackPanel.
<ListView x:Name="MyListView" Margin="0,106,0,0">
<ListView.Resources>
<Style TargetType="ListViewItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
</Style>
</ListView.Resources>
<ListView.View>
<GridView>
<GridViewColumn Header="Num" Width="170">
<GridViewColumn.CellTemplate>
<DataTemplate>
<ComboBox />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
<ListViewItem></ListViewItem>
</ListView>

Resources