Skip to main content
Home Forums Silverlight Programming Programming with .NET - General Simple Spread Control using Grid Panel
4 replies. Latest Post by philsal on August 11, 2009.
(0)
philsal
Member
85 points
124 Posts
08-10-2009 11:56 PM |
Hi,
I need a spreadsheet type control to display data. The data varies at runtime so the creation/population needs to occur at runtime. I have figured out how to handle except how to freeze the column/row headers. My xaml is below. I am using three grids because I thought I could freeze the panels, but I cant get it to work. The issue is that when a horizontal scroll occurs, I want the column headers to scroll but not the row headers. When a vertical scroll occurs, I want the rows to scroll but not the columns. Any ideas?
<ScrollViewer HorizontalScrollBarVisibility="Visible" VerticalScrollBarVisibility="Visible" Grid.Row="1"> <Grid > <Grid.RowDefinitions> <RowDefinition Height="Auto"/> <RowDefinition Height="*"/> </Grid.RowDefinitions>
<Grid.ColumnDefinitions> <ColumnDefinition Width="Auto"/> <ColumnDefinition Width="*"/> </Grid.ColumnDefinitions>
<Grid Grid.Row="0" Grid.Column="1" x:Name="ColumnHeader"></Grid> <Grid Grid.Row="1" Grid.Column="0" x:Name="RowHeader"></Grid> <Grid Grid.Row="1" Grid.Column="1" x:Name="Table"></Grid> </Grid> </ScrollViewer>
ken tucker
All-Star
16332 points
2,497 Posts
08-11-2009 6:18 AM |
Wouldn't it be easier to use the datagrid?
08-11-2009 6:52 AM |
Hi Ken The data i am dealing with is a table rather than rows of information. i really need a simple spreadsheet control
08-11-2009 8:41 AM |
Hi All
This is what I ended up doing. I wrapped each of the grids in a ScrollViewer but on the Row/Column Headers I hid the scroll bars.
I then listened on the Updated Event of the Main Scroll Viewer and did the following code
Basically what this does is wait until the silverlight control has been fully created (GetChildrenCount does this). Once its been created, I then get the First Child and search for the Horizontal and Veritical Scroll Bars. Once I have them I listed on the ValueChanged Event and change the headers to scroll along with the main scroll bar. Easy if you spend several hours searching the net finding others who have had the same issue. Thanks for your help
private void TableScroll_LayoutUpdated(object sender, EventArgs e) { if (_havescrollbar == true) return;
if (VisualTreeHelper.GetChildrenCount(TableScroll) == 0) return;
FrameworkElement f = (FrameworkElement)VisualTreeHelper.GetChild(TableScroll, 0); VerticalScroll = (System.Windows.Controls.Primitives.ScrollBar)f.FindName("VerticalScrollBar"); HorizontalScroll = (System.Windows.Controls.Primitives.ScrollBar)f.FindName("HorizontalScrollBar"); _havescrollbar = true;
if (VerticalScroll != null) VerticalScroll.ValueChanged += new RoutedPropertyChangedEventHandler<double>(VerticalScroll_ValueChanged);
if (HorizontalScroll != null) HorizontalScroll.ValueChanged += new RoutedPropertyChangedEventHandler<double>(HorizontalScroll_ValueChanged);
}
void VerticalScroll_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e) { RowScroll.ScrollToVerticalOffset(e.NewValue); }
void HorizontalScroll_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e) { ColumnScroll.ScrollToHorizontalOffset(e.NewValue); }
08-11-2009 8:43 AM |
Here is my xaml as well
<Grid Grid.Row="1"> <Grid.RowDefinitions> <RowDefinition Height="Auto"/> <RowDefinition Height="*"/> </Grid.RowDefinitions>
<ScrollViewer x:Name="ColumnScroll" Grid.Row="0" Grid.Column="1" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden" BorderThickness="0" > <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="*"/> <ColumnDefinition Width="16"/> </Grid.ColumnDefinitions> <Grid x:Name="ColumnHeader" LayoutUpdated="ColumnHeader_LayoutUpdated"></Grid> </Grid> </ScrollViewer>
<ScrollViewer x:Name="RowScroll" Grid.Row="1" Grid.Column="0" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden" BorderThickness="0"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="*"/> <RowDefinition Height="16"/> </Grid.RowDefinitions>
<Grid x:Name="RowHeader" LayoutUpdated="RowHeader_LayoutUpdated"></Grid> </Grid> </ScrollViewer>
<ScrollViewer x:Name="TableScroll" Grid.Row="1" Grid.Column="1" HorizontalScrollBarVisibility="Visible" VerticalScrollBarVisibility="Visible" BorderThickness="0" LayoutUpdated="TableScroll_LayoutUpdated" > <Grid x:Name="Table"></Grid> </ScrollViewer> </Grid>