I can get events from everything under the headers, but I can't get an event from clicking the headers. Here's the XAML; notice the event is for the entire ListView, so it should activate when anything is clicked:
<ListView x:Name="myListView" MouseLeftButtonUp="myListView_MouseLeftButtonUp" Margin="10">
<ListView.View>
<GridView>
<GridView.Columns>
<GridViewColumn Header="File Path"
DisplayMemberBinding="{Binding Path=Path}" />
<GridViewColumn Header="File Size"
DisplayMemberBinding="{Binding Path=Size}" />
</GridView.Columns>
</GridView>
</ListView.View>
</ListView>
And the event itself is very simple. Just show me that something has happened:
private void myListView_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
string output = sender.ToString();
MessageBox.Show(output);
}
Clicking anywhere under the headers responds perfectly:
"System.Windows.Controls.ListView Items.Count:0"
Clicking the "File Path" header does nothing. Clicking the "File Size" header does nothing.
MSDN says:
https://msdn.microsoft.com/en-us/library/vstudio/ms745786(v=vs.100).aspx
<ListView x:Name='lv' Height="150" HorizontalAlignment="Center" VerticalAlignment="Center"
GridViewColumnHeader.Click="GridViewColumnHeaderClickedHandler">
Visual Studio says there's no such thing as GridViewColumnHeader, so none of the code on MSDN works.
That's how WPF UI events work by default. They bubble up. If somebody eats the message along the way (which is what button type controls do), the higher level controls won't get it. You can either use the Preview version of the event, or the cleaner way to do it:
AddHandler(GridViewColumnHeader.ClickEvent, new RoutedEventHandler(ListView_OnColumnClick));
<GridViewColumn Width="150" >
<GridViewColumn.Header>
<GridViewColumnHeader Click="GridViewColumnHeader_Click">click me</GridViewColumnHeader>
</GridViewColumn.Header>
</GridViewColumn>
Even if a bit late, but in XAML you can do something like:
<!-- ListView -->
<GridView AllowsColumnReorder="True">
<GridViewColumn Width="220">
<GridViewColumnHeader Content="{DynamicResource ResourceKey=explorer_column_name}" Command="{Binding Path=ISortBy}" CommandParameter="{x:Static enu:SortType.Name}"/>
<GridViewColumn.CellTemplate>
//...
</GridViewColumn.CellTemplate>
</GridViewColumn>
//..
</GridView>
AddHandler should be called on the ListView, like:
myListView.AddHandler(GridViewColumnHeader.ClickEvent, new RoutedEventHandler(myListView_OnColumnClick));
Then e.OriginalSource can be casted to GridViewColumnHeader in your myListView_OnColumnClick and you can identify your column.
Related
I have a ListView that gets its values from a database, with the last column being buttons. What I am trying to do, is that when a button is clicked, the value of the Name in the same row will be returned.
Here's the relevant part of my XAML code:
<Grid Grid.ColumnSpan="2">
<ListView Name="PeopleList">
<ListView.View>
<GridView>
<GridViewColumn Header="Name" DisplayMemberBinding="{Binding [Name]}" />
<GridViewColumn Header="Phone" DisplayMemberBinding="{Binding [Phone]}" />
<GridViewColumn Header="Email" DisplayMemberBinding="{Binding [Email]}" />
<GridViewColumn Header="Actions" Width="200">
<GridViewColumn.CellTemplate>
<DataTemplate>
<Button Content=">" Click="ActionButton_Click"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
</Grid>
You should be able to get the exact Button that was clicked from the sender argument of the Click event. From there you should be able to get Button.DataContext, which would hold a reference to the item that is being displayed on that row.
It would be something like:
private void ActionButton_Click(object sender, EventArgs args)
{
var rowItem = (sender as Button).DataContext as People;
string name = rowItem.Name;
}
I'm making a WPF app, this app has a UserControl, which has a list view.
I tried to create a click event listener but i never got it to work right, and i havend find anything to solve it
I fill this list view items with an object like this:
List<AsesoriaClass> listaAsesorias = phpClass.getListaAsesoriasAsesor(asesor.ID);
foreach (var asesoria in listaAsesorias)
{
AsesoriaTable data = new AsesoriaTable(asesoria.AsesoriaID.ToString(), asesoria.ClienteNombre + " " + asesoria.ClienteApellidos, asesoria.FechaInicio.ToString(), asesoria.FechaFinal.ToString());
this.ListView.Items.Add(data);
}
And this is the XAML of the User Control:
<ListView x:Name="ListView">
<ListView.View>
<GridView>
<GridViewColumn Header="ID" DisplayMemberBinding="{Binding Path=Id}" Width="100"/>
<GridViewColumn Header="Cliente" DisplayMemberBinding="{Binding Path=Cliente}" Width="300"/>
<GridViewColumn Header="Inicio" DisplayMemberBinding="{Binding Path=Inicio}" Width="200"/>
<GridViewColumn Header="Final" DisplayMemberBinding="{Binding Path=Final}" Width="200"/>
</GridView>
</ListView.View>
</ListView>
I want to make a click listener so, when i click on an item something happens (to start i just want it to show a mesage box). how do i do this?
You can simply attach an Event Handler on SelectionChanged. Like this
ListView.SelectionChanged += LstOnSelectionChanged;
Where LstOnSelectionChanged is a method.
private void LstOnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
MessageBox.Show("Anything");
}
Been messing with this and can't seem to figure it out. It seems that the object sender is actually the ListView.. so even though if I figure out how to get the GridViewColumn as a child... I don't know how to distinguish which column was actually clicked?
<ListView x:Name="SingleTweet_ListView"
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
MouseLeftButtonUp="SingleTweet_ListView_MouseLeftButtonUp"
Height="200" Margin="5,5,5,5" FontSize="12"
GridViewColumnHeader.Click="GridViewColumnHeaderClickedHandler">
<ListView.View>
<GridView>
<GridViewColumn Header="Screen Name"
DisplayMemberBinding="{Binding Col1}" Width="112"/>
<GridViewColumn Header="Tweet"
DisplayMemberBinding="{Binding Col2}" Width="623"/>
</GridView>
</ListView.View>
</ListView>
void GridViewColumnHeaderClickedHandler(object sender, RoutedEventArgs e)
{
MessageBox.Show(((GridViewColumn)sender).Header.ToString());
//error cus the sender is actually the listview?
}
Sender will be ListView but you can check for OriginalSource which will be GridViewColumnHeader and can get column from it:
MessageBox.Show(((GridViewColumnHeader)e.OriginalSource).Column
.Header.ToString());
I have a user control that contains a listview. In the windows where I use this usercontrol, I want to be able to do something like what is shown below where I set the template for a control within the user control.
<ct:AutoCompleteComboBox
SelectedValue="{Binding Path=SelectedCountry}"
ItemsSource="{Binding Path=CountryList}" >
<ct:AutoCompleteComboBox.DropDownList >
<ListView
Background="Blue"
MinWidth="150"
MaxHeight="200">
<ListView.View>
<GridView>
<GridView.Columns>
<GridViewColumn Header=" Name" DisplayMemberBinding="{Binding Name}" >
</GridViewColumn>
<GridViewColumn Header="Desc" DisplayMemberBinding="{Binding Description}" >
</GridViewColumn>
</GridView.Columns>
</GridView>
</ListView.View>
</ListView>
</ct:AutoCompleteComboBox.DropDownList>
</ct:AutoCompleteComboBox>
How do I go about doing this? I found that if I made AutoCompleteComboBox.DropDownList a dependency property, then the code would compile. However, this DropDownList property is not actually added to my control (it is added to the parent container of ct:AutoCompleteComboBox).
Any idea of the proper way to do this?
Thanks in advance
Bind subcontrol's property you want to be available outside to the property you created in the container control.
How to display treeview inside a list view, actually this listview has headers namely workbook, description,date, so when I display the tree view inside the listview, the treeview should open as per the listview headers.
My question is :
1. How to insert treeview inside Listview and my xaml code is
<ScrollViewer VerticalScrollBarVisibility="Auto">
<ListView Name="lstViewWorkbooks" HorizontalAlignment="Left" >
<ListView.View >
<GridView >
<GridViewColumn Header="WorkBook" Width="200" DisplayMemberBinding="{Binding AnyWorkbook}" />
<GridViewColumn Header="Description" Width="660" DisplayMemberBinding="{Binding DescName}" />
<GridViewColumn Header="Date" Width="183" DisplayMemberBinding="{Binding WorkbookDate}" />
</GridView>
</ListView.View>
<TreeView HorizontalAlignment="Left" Margin="195,76,0,-76" Name="tviewPojectView" Width="120" />
</ListView>
</ScrollViewer>
I am using MVP architecture, with C# and wpf.
The treeview in the above xaml code is enough to display the data?
Please help me
Thanks
Ramm
If you want the tree view inside a cell in your grid, you need to place it inside the cell template for the correct column like this:
<GridViewColumn Header="WorkBook" Width="200" DisplayMemberBinding="{Binding AnyWorkbook}">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TreeView HorizontalAlignment="Left" Margin="195,76,0,-76" Name="tviewPojectView" Width="120" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>