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.
Related
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>
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/
I have a WPF project developed with VS 2008 that is using version 3.5 of the .NET framework. The initial window width is set to 680 and SizeToContent is set to "Width" only. There are 3 ListView controls that are placed in a grid panel in this window. The first 2 grids both have one column where the width is specified. The 3rd ListView control does not specify the width of any column. When the ListView controls are initially filled with data, the window is properly resized. If additional data comes in to the 3rd ListView, then the window shrinks or grows the width appropriately. However, if the 1st or 2nd ListView container grows or shrinks - especially if the scrollbars are removed by increasing the window size vertically, then the width of the window is not correctly updated. Does anyone have any ideas as to how I can get the window to resize properly when the 2nd and 3rd ListView controls are updated?
Here is my XAML code for the window and ListView controls:
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:AP2"
x:Class="AP2.MainWindow"
x:Name="LobbyWindow"
Title="Lobby"
Height="480"
mc:Ignorable="d"
SizeToContent="Width"
MinWidth="680"
MinHeight="480"
SizeChanged="LobbyWindow_SizeChanged"
Loaded="LobbyWindow_Loaded"
Closing="LobbyWindow_Closing"
StateChanged="LobbyWindow_StateChanged">
<Window.Background>
<LinearGradientBrush EndPoint="0.5,1"
StartPoint="0.5,0">
<GradientStop Color="#FF434D7A"
Offset="0" />
<GradientStop Color="#FF180CFA"
Offset="1" />
</LinearGradientBrush>
</Window.Background>
...
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="0.064*" />
<RowDefinition Height="0.936*" />
</Grid.RowDefinitions>
<!-- Define the selected table label. -->
<Label x:Name="SelTableLabel"
Margin="1,-5,0,0"
Content="Table"
FontSize="11"
Foreground="#FF000000"
Background="#FFFFFFFF"
Grid.Row="0"
Grid.Column="2"
FontFamily="Georgia"
FontWeight="Bold"
VerticalAlignment="Bottom" />
<ListView x:Name="ListView1"
ItemContainerStyle="{StaticResource alternatingListViewItemStyle}"
AlternationCount="2"
SelectionChanged="ListView1_SelectionChanged"
Grid.Row="1"
Grid.Column="0"
SelectionMode="Multiple"
ItemsSource="{Binding ElementName=LobbyWindow, Path=ListCollection1}">
<ListView.View>
<GridView>
<GridViewColumn DisplayMemberBinding="{Binding Game}">
<GridViewColumnHeader Content="Game"
FontWeight="Bold" />
</GridViewColumn>
<GridViewColumn DisplayMemberBinding="{Binding Stakes}">
<GridViewColumnHeader Content="Stakes"
FontWeight="Bold" />
</GridViewColumn>
<GridViewColumn Width="30"
DisplayMemberBinding="{Binding Seats}">
<GridViewColumnHeader Content="Seats"
FontWeight="Bold" />
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
<ListView x:Name="ListView2"
ItemContainerStyle="{StaticResource alternatingListViewItemStyle}"
AlternationCount="2"
Grid.Row="1"
Grid.Column="1"
SelectionChanged="ListView2_SelectionChanged"
SelectionMode="Single"
ItemsSource="{Binding ElementName=LobbyWindow, Path=ListCollection2}">
<ListView.View>
<GridView>
<GridViewColumn Width="90"
x:Name="TableName"
DisplayMemberBinding="{Binding TableName}">
<GridViewColumnHeader Content="Table"
Width="90"
FontWeight="Bold"
HorizontalAlignment="Left" />
</GridViewColumn>
<GridViewColumn DisplayMemberBinding="{Binding Stakes}">
<GridViewColumnHeader Content="Stakes"
FontWeight="Bold" />
</GridViewColumn>
<GridViewColumn DisplayMemberBinding="{Binding NumPlayers}">
<GridViewColumnHeader Content="Players"
FontWeight="Bold" />
</GridViewColumn>
<GridViewColumn DisplayMemberBinding="{Binding Speed}">
<GridViewColumnHeader Content="Speed"
FontWeight="Bold" />
</GridViewColumn>
<GridViewColumn DisplayMemberBinding="{Binding HandsPerHour}">
<GridViewColumnHeader Content="H/Hr"
FontWeight="Bold" />
</GridViewColumn>
<GridViewColumn DisplayMemberBinding="{Binding AvgPotSize}">
<GridViewColumnHeader Content="Avg Pot"
FontWeight="Bold" />
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
<ListView x:Name="ListView3"
ItemContainerStyle="{StaticResource alternatingListViewItemStyle}"
AlternationCount="2"
Grid.Row="1"
Grid.Column="2"
SelectionMode="Single"
ItemsSource="{Binding ElementName=LobbyWindow, Path=ListCollection3}">
<ListView.View>
<GridView>
<GridViewColumn DisplayMemberBinding="{Binding Player}">
<GridViewColumnHeader Content="Player"
FontWeight="Bold" />
</GridViewColumn>
<GridViewColumn DisplayMemberBinding="{Binding City}">
<GridViewColumnHeader Content="City"
FontWeight="Bold" />
</GridViewColumn>
<GridViewColumn DisplayMemberBinding="{Binding Chips}">
<GridViewColumnHeader Content="Chips"
FontWeight="Bold" />
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
</Grid>