Search code examples
c#wpfdata-bindingnavigationitemscontrol

access-like data navigation in WPF?


What would be the best way to build a data-navigation like in access-forms in XAML/C#?

Should I build a user control (or even custom control) that I just bind to my collection in which I can put other controls? (hence this question: C# User Control that can contain other Controls (when using it) )

Or can I build something by deriving from then ItemsControl somehow? how?

Or would this be done completely different today (like "this style of navigation is so last year!")?

I'm relatively new to C# and all (not programming as such, but with more like "housewife-language" Access-VBA) also I'm no native english speaker. So pls be gentle =)


Solution

  • You can create user control and place a bunch of buttons (First, Prev, Next, Last, etc..) in it and place it on the main window. Secondly, you can bind your data navigation user control to a CollectionViewSource which will help you to navigate among your data.

    Your main window:

    <Window.Resources>
        <CollectionViewSource x:Key="items" Source="{Binding}" />
    </Window.Resources>
    <Grid>
        <WpfApplication1:DataNavigation DataContext="{Binding Source={StaticResource items}}" />
        <StackPanel>
            <TextBox Text="{Binding Source={StaticResource items},Path=Name}" />
        </StackPanel>
    </Grid>
    

    Your Data Navigation User Control:

    <StackPanel>
        <Button x:Name="Prev" Click="Prev_Click">&lt;</Button>
        <Button x:Name="Next" Click="Next_Click">&gt;</Button>
        <!-- and so on -->
    </StackPanel>
    

    And your click handlers goes like this:

    private void Prev_Click(object sender, RoutedEventArgs e)
    {
        ICollectionView view = CollectionViewSource.GetDefaultView(DataContext);
        if (view != null)
        {
            view.MoveCurrentToPrevious();
        }
    }
    

    I hope this helps.