問題描述
在 XAML/C# 中的訪問表單中構建數據導航的最佳方式是什么?
What would be the best way to build a data-navigation like in access-forms in XAML/C#?
我是否應該構建一個用戶控件(甚至是自定義控件),然后將其綁定到我的集合中,以便在其中放置其他控件?(因此這個問題:C# User Control that can包含其他控件(使用時))
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) )
或者我可以通過某種方式從 ItemsControl 派生來構建一些東西嗎?怎么樣?
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!")?
我對 C# 和所有東西都比較陌生(不是這樣編程,而是更像家庭主婦語言"Access-VBA),而且我不是以英語為母語的人.所以請溫柔=)
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 =)
推薦答案
您可以創建用戶控件并在其中放置一堆按鈕(First、Prev、Next、Last 等)并將其放置在主窗口上.其次,您可以將數據導航用戶控件綁定到 CollectionViewSource
,這將幫助您在數據之間導航.
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.
您的主窗口:
<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"><</Button>
<Button x:Name="Next" Click="Next_Click">></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();
}
}
我希望這會有所幫助.
這篇關于WPF中類似訪問的數據導航?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!