Ok so I've been looking for a WPF listview that works something like a treeview. I have a Data Model that has an observerableCollection of children of the item. I've looked at a few options using a few treeview with columns controls that have been shown on stackoverflow or on CodeProject but they either aren't bindable or are too slow to be useful.
The list will be update constantly throughout the day any may have more than 50k-100k items in it by the end of the day. A normal listview control seems to be able to handle these updates and still be useful without completely freezing when a resort is needed.
Now, my data objects should really only ever have one layer of children. So what I was thinking about trying is displaying a toggle button on the listview item (if the item has children), but then when the toggle button is clicked I want to display another listview of that item's children within the current listviewitem. I've been searching for more than a week trying to figure out how to do this. Can anyone lead me in the right direction here? I'm thinking I need a ItemTemplate for the listview but I'm not exactly sure how to get the child listview to show in the parent's listviewitem.
Thanks in advance
UPDATE:
Here is a sample of the XAML I'm currently trying to use but it doesn't seem to be working correctly. I want the columns but when I click the toggle button I want the sub listview to be displayed. Currently all I see is the columns and I can't get the sub listview to display.
<ListView ItemsSource="{Binding MyCollection, NotifyOnSourceUpdated=True, NotifyOnTargetUpdated=True, UpdateSourceTrigger=PropertyChanged}" x:Name="lsvMyList">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<ListView ItemsSource="{Binding Children}">
<ListView.View>
<GridView>
<GridViewColumn Header="Column2"
DisplayMemberBinding="{Binding Column2}"
Width="100"/>
<GridViewColumn Header="Column3"
DisplayMemberBinding="{Binding Column3}"
Width="100"/>
<GridViewColumn Header="Column5"
DisplayMemberBinding="{Binding Column5}"
Width="100"/>
<GridViewColumn Header="Column8"
DisplayMemberBinding="{Binding Column8}"/>
</GridView>
</ListView.View>
</ListView>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
<ListView.View>
<GridView>
<GridViewColumn Width="68" Header="IsExpanded">
<GridViewColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<ToggleButton Visibility="{Binding ChildCount, Converter={StaticResource Toggle}}"
x:Name="tbnItemExpander"
Content="+"
Height="15"
Width="15"
BorderThickness="0"
Background="Transparent"
IsChecked="{Binding IsExpanded, NotifyOnSourceUpdated=True, NotifyOnTargetUpdated=True, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
HorizontalContentAlignment="Center"
VerticalContentAlignment="Center"
Click="Toggle_Click"/>
<TextBlock VerticalAlignment="Center"
Text="{Binding ChildCount, NotifyOnSourceUpdated=True, NotifyOnTargetUpdated=True, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource RejectCount2}}"
Margin="2,0,2,0"/>
</StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Column2"
DisplayMemberBinding="{Binding Column2, NotifyOnSourceUpdated=True, NotifyOnTargetUpdated=True, UpdateSourceTrigger=PropertyChanged}"
Width="63"/>
<GridViewColumn Header="Column3"
DisplayMemberBinding="{Binding Column2, NotifyOnSourceUpdated=True, NotifyOnTargetUpdated=True, UpdateSourceTrigger=PropertyChanged}"
Width="28"/>
<GridViewColumn Header="Column4"
DisplayMemberBinding="{Binding Column2, NotifyOnSourceUpdated=True, NotifyOnTargetUpdated=True, UpdateSourceTrigger=PropertyChanged}"
Width="68"/>
<GridViewColumn Header=""
Width="110">
<GridViewColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Button Click="Button_Click"
Margin="2" Padding="2"
IsEnabled="{Binding Column5, Converter={StaticResource IsNull}}"
Content="{Binding Converter={StaticResource btnContent}}"
Tag="{Binding NotifyOnSourceUpdated=True, NotifyOnTargetUpdated=True, UpdateSourceTrigger=PropertyChanged}"/>
</StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
Related
How do I insert a horizontal line after each list view item in a grid?
<Grid>
<ListView Margin="10" Name="Users">
<ListView.View>
<GridView>
<GridViewColumn Header="Name" Width="300">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock TextWrapping="Wrap" TextTrimming="WordEllipsis" Height="32" Text="{Binding Name}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Age" Width="200">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock TextWrapping="Wrap" TextTrimming="WordEllipsis" Height="32" Text="{Binding Age}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
</Grid>
I tried defining Border after DataTemplate
<DataTemplate>
<Border Margin="5" BorderThickness="1" BorderBrush="SteelBlue">
but that only added a border around each of the data template items. How do I insert a horizontal separator after each row?
I think you ought to be able to set a row style via the ItemContainerStyle:
https://stackoverflow.com/a/4474474/424129
You'd want to set a BorderThickness="0,0,0,1" to have only a bottom border.
How to create Check box for Column inside Listview. i was able to make checkbox for listview items. but i want to have checkbox for Column itself.like in windows:
here is code in XAML
<ListView HorizontalAlignment="Left" Grid.Row="1" Width="400">
<ListView.View>
<GridView>
<GridViewColumn Width="140" Header="Column1 With Checkbox">
<GridViewColumn.CellTemplate>
<DataTemplate>
<CheckBox Tag="{Binding}" IsThreeState="False" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Width="140" Header="Column2" />
<GridViewColumn Width="115" Header="Column3" />
</GridView>
</ListView.View>
</ListView>
Note that this will only make checkboxes for items not Column itself. so how to make this happen?
You can make the header a custom control by defining it under the GridViewColumn.Header property.
<ListView HorizontalAlignment="Left" Grid.Row="1" Width="400">
<ListView.View>
<GridView>
<GridViewColumn Width="140">
<GridViewColumn.Header>
<StackPanel Orientation="Horizontal">
<Checkbox IsChecked="{Binding YourCheckedProperty}" />
<TextBlock Text="Column1" />
</StackPanel>
<GridViewColumn.Header>
</GridViewColumn>
<GridViewColumn Width="140" Header="Column2" />
<GridViewColumn Width="115" Header="Column3" />
</GridView>
</ListView.View>
</ListView>
I'm trying to group my items in Listview. I want to group them by "EventId" property and show EventId as header text. Groupping is working perfect, my items are groupped but the problem is, header text is empty for all groups.
Here is my xaml:
<ListView
ItemsSource="{Binding CardEvents}"
MinHeight="120"
MaxHeight="120"
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
ScrollViewer.VerticalScrollBarVisibility="Auto"
Name="CardEventsListView">
<ListView.View>
<GridView>
<GridViewColumn Header="Kart Türü" DisplayMemberBinding="{Binding CardType}" Width="{Binding ActualWidth, ElementName=cardEvent_headerA}"/>
<GridViewColumn Header="Oluşturma Tarihi" DisplayMemberBinding="{Binding EventDateTime, Converter={StaticResource dateTimeConverter}}" Width="{Binding ActualWidth, ElementName=cardEvent_headerB}"/>
<GridViewColumn Header="Event Id" DisplayMemberBinding="{Binding EventId}" Width="{Binding ActualWidth, ElementName=cardEvent_headerC}"/>
<GridViewColumn Header="Dakika" DisplayMemberBinding="{Binding Minute}" Width="{Binding ActualWidth, ElementName=cardEvent_headerD}"/>
<GridViewColumn Header="İşlem Dk" DisplayMemberBinding="{Binding RelativeMinute, Converter={StaticResource ResourceKey=timeSpanConverter}}" Width="{Binding ActualWidth, ElementName=cardEvent_headerE}"/>
<GridViewColumn Header="Op." DisplayMemberBinding="{Binding UserName}" Width="{Binding ActualWidth, ElementName=cardEvent_headerF}"/>
</GridView>
</ListView.View>
<ListView.GroupStyle>
<GroupStyle>
<GroupStyle.HeaderTemplate>
<DataTemplate>
<TextBlock FontSize="14" FontWeight="Bold" Text="{Binding EventId}" />
</DataTemplate>
</GroupStyle.HeaderTemplate>
</GroupStyle>
</ListView.GroupStyle>
In my Textblock, where i do the binding i get warning
Cannot resolve symbol 'EventId'
I don't know why i can't access to EventId property while i can access it in GridViewColumn.
You cannot bind EventId of the ItemSource to your Group's DataTemplate, instead try using Name like
<DataTemplate>
<TextBlock FontSize="14" FontWeight="Bold" Text="{Binding Name}" />
</DataTemplate>
Here Name is the name of the group, as assigned by WPF but not from DataModel.
Reference - http://www.wpf-tutorial.com/listview-control/listview-grouping/
There probably was a topic in which lays the solution for my problem but I've spend too much hours on this so I've decided to ask.
I have a ListView to which I've binded a collection of processes. And I have a ListBox in which I want to place Threads of a selected process. Binding in ListView works, but analogical in ListBox doesn't.
Here's my .xaml:
<Window x:Class="TaskManager.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window" Height="453" Width="533" Icon="/TaskManager;component/Images/taskmgr.png">
<Grid>
<ListView Height="276" HorizontalAlignment="Left" Name="processesView" VerticalAlignment="Top" Width="503" ItemsSource="{Binding ProcessList}">
<ListView.View>
<GridView AllowsColumnReorder="False">
<GridViewColumn DisplayMemberBinding="{Binding ProcessName}">
<GridViewColumn.Header>
<GridViewColumnHeader Click="nameColumnHeader_Click">
Name
</GridViewColumnHeader>
</GridViewColumn.Header>
</GridViewColumn>
<GridViewColumn DisplayMemberBinding="{Binding Id}">
<GridViewColumn.Header>
<GridViewColumnHeader Click="IDColumnHeader_Click">
ID
</GridViewColumnHeader>
</GridViewColumn.Header>
</GridViewColumn>
<GridViewColumn DisplayMemberBinding="{Binding BasePriority}">
<GridViewColumn.Header>
<GridViewColumnHeader Click="priorityColumnHeader_Click">
Priority
</GridViewColumnHeader>
</GridViewColumn.Header>
</GridViewColumn>
<GridViewColumn Header="Keep alive">
<GridViewColumn.CellTemplate>
<DataTemplate>
<CheckBox x:Name="keepAliveBox" IsChecked="{Binding EnableRaisingEvents}" Checked="keepAliveBox_Checked" Unchecked="keepAliveBox_Unchecked" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
<ListBox Height="120" HorizontalAlignment="Left" Margin="93,282,0,0" Name="threadBox" VerticalAlignment="Top" Width="200" ItemsSource="{Binding ElementName=processesView, Path=SelectedItem.Threads}">
<ListBox.ItemTemplate>
<DataTemplate>
<!--<ListBoxItem Content="{Binding}"/>-->
<Label Content="{Binding Name}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
After execution I can't see anything in the ListBox but if I click inside something is selected. So if I change the above to Content="TEST" I can see several Labels with "TEST" value. So it seems that I'm binding the name somehow incorrectly.
Here's the .cs part where I set the context:
public MainWindow()
{
InitializeComponent();
processesView.DataContext = this;
threadBox.DataContext = this;
...
}
Items were invisible but selectable so it was probable that I was binding the wrong thing. And that's the reason. I was binding to Name property of Thread whilst Process has a collection of ProcessThreads which don't have that property. Here's the fix in .xaml:
<ListBox Height="120" HorizontalAlignment="Left" Margin="93,282,0,0" Name="threadBox" VerticalAlignment="Top" Width="200" ItemsSource="{Binding ElementName=processesView, Path=SelectedItem.Threads}">
<ListBox.ItemTemplate>
<DataTemplate>
<Label Content="{Binding Path=Id}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Can't wait for XAML debugger in Visual Studio 2015.
I wrote XAML as follows:
<UserControl.Resources>
<GridViewColumnCollection x:Key="gvcc">
<GridViewColumn Header="Klient"
DisplayMemberBinding="{Binding CustomerName}"
Width="80">
</GridViewColumn>
<GridViewColumn Header="Budżet"
DisplayMemberBinding="{Binding Budget, StringFormat={}{0:C}}"
Width="80">
</GridViewColumn>
<GridViewColumn Header="Zysk. zakł."
DisplayMemberBinding="{Binding ExpectedProfability, StringFormat={}{0:C}}"
Width="80">
</GridViewColumn>
<GridViewColumn Header="Zysk. real."
DisplayMemberBinding="{Binding RealProfability, StringFormat={}{0:C}}"
Width="80">
</GridViewColumn>
<GridViewColumn Header="Miejsce"
DisplayMemberBinding="{Binding Place}"
Width="80" />
<GridViewColumn Header="Nr proj."
DisplayMemberBinding="{Binding Number}"
Width="80">
</GridViewColumn>
</GridViewColumnCollection>
</UserControl.Resources>
...
<Grid>
<ScrollViewer ScrollViewer.VerticalScrollBarVisibility="Visible">
<ItemsControl ItemsSource="{Binding GroupedProjects}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="70" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Border Grid.Column="0" Grid.Row="0" Grid.RowSpan="3" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Background="Red" >
<TextBlock FontSize="13" Text="{Binding Month}" TextAlignment="Center" VerticalAlignment="Center" HorizontalAlignment="Center" />
</Border>
<GridViewHeaderRowPresenter Name="hrp" Columns="{StaticResource gvcc}" Grid.Column="1" Grid.Row="0" />
<ListBox ItemsSource="{Binding Details}" Grid.Column="1" Grid.Row="1" ScrollViewer.VerticalScrollBarVisibility="Disabled"
GotFocus="ListBox_GotFocus"
PreviewMouseWheel="ListBox_PreviewMouseWheel"
SelectedItem="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=DataContext.SelectedProject}">
<ListBox.ItemTemplate>
<DataTemplate>
<GridViewRowPresenter Columns="{StaticResource gvcc}" Height="24" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<!-- HERE IS THE PROBLEM -->
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Vertical"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</ScrollViewer>
</Grid>
Data are grouped in the ListBox, underneath the ListBox I want to display something like a summary row. I wanted to do this row as the StackPanel with TextBlock controls. The problem is - how to relate the width of the TextBox controls with the widths of columns in the ListBox.
Let's say the "summary row" is something like that:
<StackPanel Orientation="Horizontal">
<TextBlock Width="bind to ListBox.Column1.Width + ListBox.Column2.Width">some data</TextBlock>
<TextBlock Width="bind to ListBox.Column3.Width">other data</TextBlock>
<TextBlock Width="bind to ListBox.Column4.Width">etc.</TextBlock>
</StackPanel>
As you can see - first TextBlock width is something like "ColumnSpan = 2". The width of the other TextBlocks is simply the width of a column to which I would like to pair them.
Is such a thing is even possible in XAML? Can anyone of you knows how to implement a similar solution in different way?
If you are doing this through MVVM, you can easily use property binding to get it done. Just set the width of each of the ListBox columns to properties in your ViewModel. The textBox controls widths will be bound to a seperate readonly property which takes the appropriate widths, manipulates them in some way, and returns a value. You will have to implement INotifyPropertyChanged and call it on each of the textbox width properties every time a listboxwidth property is changed.
So something like so:
Private _Column1Width As Double
Public Property Column1Width() As Double
Get
Return _Column1Width
End Get
Set(ByVal value As Double)
_Column1Width = value
OnPropertyChanged("TextBox1Width")
End Set
End Property
Private _Column2Width As Double
Public Property Column2Width() As Double
Get
Return _Column2Width
End Get
Set(ByVal value As Double)
_Column2Width = value
OnPropertyChanged("TextBox1Width")
End Set
End Property
Public ReadOnly Property TextBox1Width() As Double
Get
Return Column1Width + (Column2Width * 2)
End Get
End Property
And bindings like so:
<GridViewColumnCollection x:Key="gvcc">
<GridViewColumn Header="Klient"
DisplayMemberBinding="{Binding CustomerName}"
Width="{Binding Column1Width, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"}">
</GridViewColumn>
<GridViewColumn Header="Budżet"
DisplayMemberBinding="{Binding Budget, StringFormat={}{0:C}}"
Width="{Binding Column2Width, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
</GridViewColumn>
<GridViewColumn Header="Zysk. zakł."
DisplayMemberBinding="{Binding ExpectedProfability, StringFormat={}{0:C}}"
Width="80">
</GridViewColumn>
<GridViewColumn Header="Zysk. real."
DisplayMemberBinding="{Binding RealProfability, StringFormat={}{0:C}}"
Width="80">
</GridViewColumn>
<GridViewColumn Header="Miejsce"
DisplayMemberBinding="{Binding Place}"
Width="80" />
<GridViewColumn Header="Nr proj."
DisplayMemberBinding="{Binding Number}"
Width="80">
</GridViewColumn>
</GridViewColumnCollection>
<TextBlock Width="{Binding TextBox1Width}">some data</TextBlock>
Everything resizes just as it should continuously updating as you resize the columns.