Search code examples
c#silverlightxamlwindows-phone

DataGrid Scrolling not working


I have a datagrid which is populated using data retrieved from a database. Unfortunately, when the number of rows retrieved is more than the height of the datagrid, I cannot scroll vertically or horizontally, even though both scrollbars are shown.

Here is my code:

        <data:DataGrid x:Name="DataGrid_Transactions" AutoGenerateColumns="False" VerticalAlignment="Center" Height="300" ScrollViewer.HorizontalScrollBarVisibility="Auto" ScrollViewer.VerticalScrollBarVisibility="Auto" Background="#FF0F1111" FontSize="26" Foreground="RoyalBlue" HorizontalGridLinesBrush="AliceBlue" IsReadOnly="True" VerticalGridLinesBrush="AliceBlue" AlternatingRowBackground="AliceBlue" FontFamily="Segoe WP SemiLight" RowDetailsVisibilityMode="Visible" RowBackground="AliceBlue">
            <data:DataGrid.Columns>
                <data:DataGridTextColumn Header="Transaction ID" Binding="{Binding TransactionID}" />
                <data:DataGridTextColumn Header="Vendor Username" Binding="{Binding VendorUsername}" />
                <data:DataGridTextColumn Header="Purchase Description" Binding="{Binding PurchaseDescription}" />
                <data:DataGridTextColumn Header="Total Price" Binding="{Binding TotalPrice}" />
                <data:DataGridTextColumn Header="Currency" Binding="{Binding Currency}" />
            </data:DataGrid.Columns>
        </data:DataGrid>
        <Button Content="Main Page" Height="72" HorizontalAlignment="Center" Margin="0,540,0,43" Name="Button_MainPage" VerticalAlignment="Center" Width="456" Click="Button_MainPage_Click" />
    </Grid>
</Grid>

Can someone please help me? Thank you :)


Solution

  • It seems attached property does not work directly for this type objects, Even it works for ListBox(http://www.silverlightshow.net/items/Tip-How-to-specify-ScrollViewer-s-attached-properties-in-XAML.aspx)

    You should put it in a ScrollViewer

    <ScrollViewer ...>
           content
    </ScrollViewer>
    

    http://msdn.microsoft.com/en-us/library/system.windows.controls.scrollviewer(v=vs.95).aspx

    For another way, have a look this attached property coding tip http://www.codeproject.com/Articles/95746/Exposing-and-Binding-to-a-Silverlight-ScrollViewer

    You may also code your own attached property.(you shoud use RegisterAttached while dependency object registration,and obey attached property coding rules.)