<Window x:Class="demo.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="NavigateCollection" Height="367" Width="422" xmlns:db="clr-namespace:StoreDatabase;assembly=StoreDatabase" xmlns:local="clr-namespace:demo" > <Grid Margin="10"> <Grid.RowDefinitions> <RowDefinition Height="Auto"></RowDefinition> <RowDefinition Height="*"></RowDefinition> <RowDefinition Height="Auto"></RowDefinition> </Grid.RowDefinitions> <!--IsSynchronizedWithCurrentItem同步view显示内容--> <ComboBox Name="lstProduts" DisplayMemberPath="ModelName" Text="{Binding Path=ModelName}" IsSynchronizedWithCurrentItem="True" SelectionChanged="lstProducts_SelectionChanged"></ComboBox> <Border Grid.Row="1" Padding="5" Margin="0,5,0,5" Background="LightSteelBlue"> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto"></ColumnDefinition> <ColumnDefinition></ColumnDefinition> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="Auto"></RowDefinition> <RowDefinition Height="Auto"></RowDefinition> <RowDefinition Height="Auto"></RowDefinition> <RowDefinition Height="Auto"></RowDefinition> <RowDefinition Height="*"></RowDefinition> </Grid.RowDefinitions> <TextBlock Margin="7">Model Number:</TextBlock> <TextBox Margin="5" Grid.Column="1" Text="{Binding Path=ModelNumber}"></TextBox> <TextBlock Margin="7" Grid.Row="1">Model name:</TextBlock> <TextBox Margin="5" Grid.Row="1" Grid.Column="1" Text="{Binding Path=ModelName}"></TextBox> <TextBlock Margin="7" Grid.Row="2">Unit Cost:</TextBlock> <TextBox Margin="5" Grid.Row="2" Grid.Column="1" Text="{Binding Path=UnitCost,StringFormat={}{0:C}}"></TextBox> <TextBlock Margin="7,7,7,0" Grid.Row="3">Description:</TextBlock> <TextBox Margin="7" Grid.Row="4" Grid.Column="0" Grid.ColumnSpan="2" TextWrapping="Wrap" VerticalScrollBarVisibility="Visible" Text="{Binding Path=Description}"></TextBox> </Grid> </Border> <Grid Grid.Row="2"> <StackPanel Orientation="Horizontal"> <Button Name="cmdPrev" Click="cmdPrev_Click"><</Button> <TextBlock Margin="5,0,5,0" Name="lblPostion" VerticalAlignment="Center"></TextBlock> <Button Name="cmdNext" Click="cmdNext_Click">></Button> </StackPanel> </Grid> </Grid> </Window>
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using StoreDatabase; namespace demo { /// <summary> /// MainWindow.xaml 的交互逻辑 /// </summary> public partial class MainWindow : Window { private ICollection<Product> products; private ListCollectionView view; public MainWindow() { InitializeComponent(); //this.lstProducts.ItemsSource = App.StoreDb.GetProducts(); products = App.StoreDb.GetProducts(); this.DataContext = products; view = (ListCollectionView)CollectionViewSource.GetDefaultView(this.DataContext); view.CurrentChanged += new EventHandler(view_CurrentChanged); this.lstProduts.ItemsSource = products; } private void cmdPrev_Click(object sender, RoutedEventArgs e) { view.MoveCurrentToPrevious(); } private void cmdNext_Click(object sender, RoutedEventArgs e) { view.MoveCurrentToNext(); } private void view_CurrentChanged(object sender, EventArgs e) { this.lblPostion.Text = "Record " + (view.CurrentPosition + 1).ToString() + " of " + view.Count.ToString(); cmdPrev.IsEnabled = view.CurrentPosition > 0; cmdNext.IsEnabled = view.CurrentPosition < view.Count - 1; } //private void lstProducts_SelectionChanged(object sender, RoutedEventArgs e) //{ // view.MoveCurrentTo(lstProduts.SelectedItem); //} private void lstProducts_SelectionChanged(object sender, SelectionChangedEventArgs e) { view.MoveCurrentTo(lstProduts.SelectedItem); } } }
时间: 2024-10-13 19:59:30