XAML代码
<Page x:Class="MyList.UI.RegisterListPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" mc:Ignorable="d" xmlns:local="clr-namespace:MyList.UI" xmlns:util="clr-namespace:MyList.UI.Util" xmlns:pager="clr-namespace:FYJ.Winui.Pager;assembly=FYJ.Winui.Pager" d:DesignHeight="300" d:DesignWidth="600" Title="RegisterListPage"> <Page.Resources> <util:DatetimeConvert x:Key="datetimeConvert"></util:DatetimeConvert> </Page.Resources> <Grid> <Grid.RowDefinitions> <!--2个0高度屏蔽掉查询和分页--> <RowDefinition Height="50"/> <RowDefinition Height="0"/> <RowDefinition Height="*"/> <RowDefinition Height="0"/> </Grid.RowDefinitions> <DockPanel Grid.Row="0"> <local:ToolBarUserControl x:Name="toolBarControl1"></local:ToolBarUserControl> </DockPanel> <WrapPanel Grid.Row="1" Margin="0,5,5,0"> <TextBlock Text="关键字" Margin="5,0,0,0" VerticalAlignment="Center"></TextBlock> <TextBox Width="100" Margin="5,0,0,0" Height="24" VerticalAlignment="Center" x:Name="txtQuery"></TextBox> <TextBlock Text="日期始" Margin="5,0,0,0" VerticalAlignment="Center"></TextBlock> <DatePicker Margin="5,0,0,0" Width="100" VerticalAlignment="Center" x:Name="dateStart"></DatePicker> <TextBlock Text="日期终" Margin="5,0,0,0" VerticalAlignment="Center"></TextBlock> <DatePicker Margin="5,0,0,0" Width="100" VerticalAlignment="Center" x:Name="dateEnd"></DatePicker> <Button Content="查询" Margin="5,0,0,0" VerticalAlignment="Center" Click="Search_Click"></Button> </WrapPanel> <DataGrid Grid.Row="2" x:Name="dataGrid1" AutoGenerateColumns="False" IsReadOnly="True" AlternationCount="2" ItemsSource="{Binding QueryData}" util:SelectedItemsBehavior.SelectedItemsChangedHandler="{Binding DeleteCommand}"> <DataGrid.Columns> <DataGridTextColumn Binding="{Binding Path=registerUrlName}" ClipboardContentBinding="{x:Null}" Header="网站名称"/> <DataGridHyperlinkColumn Binding="{Binding Path=registerUrl}" ClipboardContentBinding="{x:Null}" Header="网址"/> <DataGridTextColumn Binding="{Binding Path=registerUserName}" ClipboardContentBinding="{x:Null}" Header="用户名"/> <DataGridTextColumn Binding="{Binding Path=registerEmail}" ClipboardContentBinding="{x:Null}" Header="邮箱"/> <DataGridTextColumn Binding="{Binding Path=bindTel}" ClipboardContentBinding="{x:Null}" Header="绑定电话"/> <DataGridTextColumn Binding="{Binding Path=bindQQ}" ClipboardContentBinding="{x:Null}" Header="绑定QQ"/> <DataGridTextColumn Binding="{Binding Path=bindWeibo}" ClipboardContentBinding="{x:Null}" Header="绑定微博"/> <DataGridTextColumn Binding="{Binding Path=ADD_DATE,Converter={StaticResource datetimeConvert}}" ClipboardContentBinding="{x:Null}" Header="添加时间"/> <DataGridTextColumn Binding="{Binding Path=UPDATE_DATE,Converter={StaticResource datetimeConvert}}" ClipboardContentBinding="{x:Null}" Header="修改时间"/> </DataGrid.Columns> <DataGrid.RowStyle> <Style TargetType="{x:Type DataGridRow}"> <Style.Triggers> <Trigger Property="ItemsControl.AlternationIndex" Value="0"> <Setter Property="Background" Value="#FFF0F0F0" /> </Trigger> </Style.Triggers> </Style> </DataGrid.RowStyle> <!-- 必须设置CellStyle 否则上面的IsSelected不起作用 因为CellStyle会覆盖RowStyle--> <DataGrid.CellStyle > <Style TargetType="DataGridCell"> <Style.Triggers > <Trigger Property="IsSelected" Value="True"> <Setter Property="Background" Value="#FF6B8BE0"></Setter> </Trigger> </Style.Triggers> </Style> </DataGrid.CellStyle> </DataGrid> <local:LoadingWait Grid.Row="2" x:Name="loading" Visibility="{Binding LoadingVisibility}"></local:LoadingWait> <DockPanel Grid.Row="3" Margin="0,5,0,5" DataContext="{Binding ElementName=dataGrid1,Path=CurrentItem}"> <pager:PagerUserControl x:Name="pager" Total="{Binding ElementName=dataGrid1,Path=Items.Count}" PageChanged="pager_PageChanged"></pager:PagerUserControl> <!--<TextBox Height="23" TextWrapping="Wrap" Text="{Binding Path=fileName}" MinWidth="200"/>--> <!--<TextBlock x:Name="txtTotal" Text="{Binding ElementName=dataGrid1,Path=Items.Count}" VerticalAlignment="Center" ></TextBlock>--> </DockPanel> <Grid.ContextMenu> <ContextMenu> <MenuItem Header="添加" x:Name="menuAdd"> <MenuItem.Icon> <Image Source="Resources/Add.png"></Image> </MenuItem.Icon> </MenuItem> <MenuItem Header="修改" x:Name="menuEdit"> <MenuItem.Icon> <Image Source="Resources/Edit.png"></Image> </MenuItem.Icon> </MenuItem> <!--Command="{Binding DeleteCommand}" CommandParameter="{Binding SelectedItems,ElementName=dataGrid1}" 无法传递参数--> <MenuItem Header="删除" x:Name="menuDelete" Command="{Binding DeleteCommand}"> <MenuItem.Icon> <Image Source="Resources/Delete.png"></Image> </MenuItem.Icon> </MenuItem> <MenuItem Header="刷新" Command="{Binding QueryCommand}"> <MenuItem.Icon> <Image Source="Resources/Refresh.png"></Image> </MenuItem.Icon> </MenuItem> <MenuItem Header="筛选..."> </MenuItem> <MenuItem Header="查找..." Command="{Binding SearchCommand}"> <MenuItem.Icon> <Image Source="Resources/search.png"></Image> </MenuItem.Icon> </MenuItem> <MenuItem Header="查找下一个" Command="{Binding SearchNextCommand}"> </MenuItem> <MenuItem Header="导出"> <MenuItem.Icon> <Image Source="Resources/export.png"></Image> </MenuItem.Icon> <MenuItem Header="导出选择..."></MenuItem> <MenuItem Header="导出当前页..."></MenuItem> <MenuItem Header="导出全部..."></MenuItem> </MenuItem> <MenuItem Header="打印..."> <MenuItem.Icon> <Image Source="Resources/print.png"></Image> </MenuItem.Icon> </MenuItem> </ContextMenu> </Grid.ContextMenu> </Grid> </Page>
当然不是所有按钮和事件都用了MVVM模式 有时候觉得也不用太纠结这个
下面是很少的后台代码
using FYJ.Pager; using MyList.Entity; using MyList.Model; using MyList.UI.Util; using MyList.ViewModel; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; 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; namespace MyList.UI { /// <summary> /// RegisterListPage.xaml 的交互逻辑 /// </summary> public partial class RegisterListPage : Page { private RegisterViewModel viewModel; public RegisterListPage() { InitializeComponent(); } //这个方法比构造函数先执行 protected async override void OnInitialized(EventArgs e) { base.OnInitialized(e); InitEvents(); viewModel = new RegisterViewModel(this.Dispatcher); viewModel.MdataGrid = this.dataGrid1; viewModel.MSearchWindow = new SearchWindow(); viewModel.LoadedEvent += viewModel_LoadedEvent; await InitData(); this.DataContext = this.viewModel; } void viewModel_LoadedEvent(int total) { this.pager.Init(total); } //查询数据 private async Task InitData() { await Task.Run(() => { viewModel.QueryCommand.Execute(null); }); } //事件 private void InitEvents() { ToolBarUserControl uc = this.toolBarControl1; Button btnAdd = (Button)uc.FindName("btnAdd"); Button btnEdit = (Button)uc.FindName("btnEdit"); btnAdd.Click += Add_Click; btnEdit.Click += Edit_Click; this.menuAdd.Click += Add_Click; this.menuEdit.Click += Edit_Click; } private void Add_Click(object sender, RoutedEventArgs e) { this.viewModel.SaveData = null; RegisterEditWindow window = new RegisterEditWindow(this.viewModel); window.ShowDialog(); } private void Edit_Click(object sender, RoutedEventArgs e) { object obj = this.dataGrid1.SelectedItem; if (obj == null) { MessageBox.Show("请选择一行数据"); return; } if (this.dataGrid1.SelectedItems.Count > 1) { MessageBox.Show("只能选择一行数据修改"); return; } this.viewModel.SaveData = new System.Collections.ObjectModel.ObservableCollection<RegisterModel>(); this.viewModel.SaveData.Add((RegisterModel)obj); RegisterEditWindow window = new RegisterEditWindow(this.viewModel); window.ShowDialog(); } private void Search_Click(object sender, RoutedEventArgs e) { GridPager pager = new GridPager(); List<Func<my_tb_register, bool>> wheres = new List<Func<my_tb_register, bool>>(); if(this.txtQuery.Text.Trim()!="") { string queryString = this.txtQuery.Text.Trim().ToLower(); wheres.Add(c=>c.bindQQ.ToLower().Contains(queryString) || c.bindTel.ToLower().Contains(queryString) ||c.bindWeibo.ToLower().Contains(queryString) ||c.registerEmail.ToLower().Contains(queryString) ||c.registerUrl.ToLower().Contains(queryString) ||c.registerUrlName.ToLower().Contains(queryString) ||c.registerUserName.ToLower().Contains(queryString) ||c.registerRemark.ToLower().Contains(queryString) ); } if (this.dateStart.Text.Trim() != "") { wheres.Add(c=>(c.ADD_DATE >= this.dateStart.DisplayDate) ||(c.UPDATE_DATE >= this.dateStart.DisplayDate)); } if (this.dateEnd.Text.Trim() != "") { wheres.Add(c => (c.ADD_DATE <= this.dateEnd.DisplayDate) || (c.UPDATE_DATE <= this.dateEnd.DisplayDate)); } } private void pager_PageChanged(object sender, FYJ.Winui.Pager.PageChangedEventArgs args) { } } }