主界面
<UserControl x:Class="TrhaCabinet.Views.Maintenances.DatasMaintenanceView" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:TrhaCabinet.Views.Maintenances" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:tai3="http://www.suntai.org/library" d:DesignHeight="450" d:DesignWidth="800" mc:Ignorable="d"> <GroupBox Header="{Binding DisplayName}" Style="{StaticResource MainModuleHeaderGroupBox}"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="auto" /> <RowDefinition /> <RowDefinition Height="auto" /> </Grid.RowDefinitions> <!-- 日志按钮 --> <Grid> <StackPanel Orientation="Horizontal"> <RadioButton Name="LoadGoods" Margin="10" Content="物品名称" FontSize="20" IsChecked="True" /> <RadioButton Name="LoadTypes" Margin="10" Content="型号名称" FontSize="20" /> <Button x:Name="ShowAddGood" Margin="10" Background="#1C86EE" Content="添加物品名称" FontSize="20" Foreground="White" Style="{StaticResource DefaultButton}" /> <Button x:Name="ShowAddType" Margin="10" Background="#1C86EE" Content="添加物品型号" FontSize="20" Foreground="White" Style="{StaticResource DefaultButton}" /> </StackPanel> </Grid> <Grid Grid.Row="1" Background="White"> <DataGrid x:Name="Goods" Grid.Row="4" Margin="20,15,20,0" VerticalAlignment="Top" AlternationCount="2" AutoGenerateColumns="False" Background="LightCyan" CanUserAddRows="False" CanUserReorderColumns="False" CanUserResizeColumns="False" CanUserResizeRows="False" CanUserSortColumns="False" FontSize="16" HeadersVisibility="Column" IsReadOnly="True" RowHeaderWidth="0" SelectionMode="Single" Visibility="{Binding Path=IsChecked, ElementName=LoadGoods, Converter={x:Static tai3:BoolVisibilityConverter.Instance}}"> <DataGrid.Columns> <DataGridTextColumn Width="*" Binding="{Binding Id}" Header="序号" IsReadOnly="True" /> <DataGridTextColumn Width="*" Binding="{Binding Name}" Header="物品名称" IsReadOnly="True" /> <DataGridTextColumn Width="*" Binding="{Binding CreatTime, StringFormat='{}{0:HH:mm}'}" Header="创建时间" IsReadOnly="True" /> <DataGridTextColumn Width="*" Header="操作" IsReadOnly="True" /> </DataGrid.Columns> </DataGrid> <DataGrid x:Name="Types" Grid.Row="4" Margin="20,15,20,0" VerticalAlignment="Top" AlternationCount="2" AutoGenerateColumns="False" Background="LightCyan" CanUserAddRows="False" CanUserReorderColumns="False" CanUserResizeColumns="False" CanUserResizeRows="False" CanUserSortColumns="False" FontSize="16" HeadersVisibility="Column" IsReadOnly="True" RowHeaderWidth="0" SelectionMode="Single" Visibility="{Binding Path=IsChecked, ElementName=LoadTypes, Converter={x:Static tai3:BoolVisibilityConverter.Instance}}"> <DataGrid.Columns> <DataGridTextColumn Width="*" Binding="{Binding Id}" Header="序号" IsReadOnly="True" /> <DataGridTextColumn Width="*" Binding="{Binding Name}" Header="物品名称" IsReadOnly="True" /> <DataGridTextColumn Width="*" Binding="{Binding CreatTime, StringFormat='{}{0:HH:mm}'}" Header="创建时间" IsReadOnly="True" /> <DataGridTextColumn Width="*" Header="操作" IsReadOnly="True" /> </DataGrid.Columns> </DataGrid> </Grid> <!-- 控制按钮 --> <StackPanel Grid.Row="2"> <Border> <Button x:Name="TryClose" Margin="5" HorizontalAlignment="Right" VerticalAlignment="Bottom" Content="返回" Style="{StaticResource BackForwardButton}" /> </Border> </StackPanel> <!--</GroupBox>--> </Grid> </GroupBox> </UserControl>
其中,按钮绑定的是x:Name="ShowAddGood"
对应的ViewModel有ShowAddGood这个事件
这个ShowAddGood事件是
public BindableCollection<DataMaintenanceGood> Goods { get; } = new BindableCollection<DataMaintenanceGood>(); public BindableCollection<DataMaintenanceType> Types { get; } = new BindableCollection<DataMaintenanceType>(); public void ShowAddGood() { var screen = IoC.Get<AddGoodViewModel>(); //windowManager.ShowDialog(screen); if (windowManager.ShowDialog(screen)==true) { Goods.Add(screen.SavedGood); } }
完整viewModel代码如下
internal class DatasMaintenanceViewModel : Screen, IMaintenance { public DatasMaintenanceViewModel(IWindowManager windowManager , IDataBaseAccessor dataBaseAccessor) { DisplayName = "数据维护"; this.windowManager = windowManager; this.dbAccessor = dataBaseAccessor; } private readonly IWindowManager windowManager; private readonly IDataBaseAccessor dbAccessor; public int Index => 6; public BindableCollection<DataMaintenanceGood> Goods { get; } = new BindableCollection<DataMaintenanceGood>(); public BindableCollection<DataMaintenanceType> Types { get; } = new BindableCollection<DataMaintenanceType>(); public void ShowAddGood() { var screen = IoC.Get<AddGoodViewModel>(); //windowManager.ShowDialog(screen); if (windowManager.ShowDialog(screen)==true) { Goods.Add(screen.SavedGood); } } public void ShowAddType() { var screen = IoC.Get<AddTypeViewModel>(); if (windowManager.ShowDialog(screen)==true) { Types.Add(screen.SavedType); } } public void LoadGoods() { Goods.Clear(); var ss = dbAccessor.Select<DataMaintenanceGood>(); Goods.AddRange(ss); } public void LoadTypes() { Types.Clear(); var ss = dbAccessor.Select<DataMaintenanceType>(); Types.AddRange(ss); } protected override void OnViewLoaded(object view) { LoadGoods(); base.OnViewLoaded(view); } }
对应的子窗体的viewModel
public class AddGoodViewModel : Screen { public AddGoodViewModel(IDataBaseAccessor dataBaseAccessor) { this.dataBaseAccessor = dataBaseAccessor; } private readonly IDataBaseAccessor dataBaseAccessor; public DataMaintenanceGood SavedGood { get; set; } #region Bind Data public int Id { get; set; } public string Name { get; set; } #endregion Bind Data #region 提交按钮 public void SubmitGood() { try { var entity = new DataMaintenanceGood() { Id = Id, Name = Name, CreatTime = DateTime.Now }; dataBaseAccessor.Insert(entity); SavedGood = entity; this.TryClose(true); } catch (Exception ex) { throw new EntityNotFoundException("添加失败:" + ex.Message); } } #endregion 提交按钮 private void Load() { int id = 1; var last = dataBaseAccessor.LastOrDefault<DataMaintenanceGood, int>(dd => dd.Id); if (last != null) { id = last.Id + 1; } Id = id; } protected override void OnViewLoaded(object view) { Load(); base.OnViewLoaded(view); } }
对应的子窗体的Xaml
<UserControl x:Class="TrhaCabinet.Views.DatasMaintenances.AddGoodView" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:TrhaCabinet.Views.DatasMaintenances" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" d:DesignHeight="450" d:DesignWidth="800" mc:Ignorable="d"> <StackPanel Orientation="Vertical"> <!--<Button x:Name="TryClose" Content="返回" DockPanel.Dock="Top" />--> <Grid> <Grid.RowDefinitions> <RowDefinition Height="auto" /> <RowDefinition Height="auto" /> <RowDefinition Height="100" /> </Grid.RowDefinitions> <GroupBox Margin="20" Header="添加物品名称"> <UniformGrid Columns="2"> <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" Text="物品序号" /> <TextBox x:Name="Id" Margin="20" Background="LightBlue" IsReadOnly="True" /> <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" Text="物品名称" /> <TextBox x:Name="Name" Margin="20" Background="LightBlue" /> </UniformGrid> </GroupBox> <UniformGrid Grid.Row="2" Columns="3"> <Button x:Name="SubmitGood" Width="100" Margin="20" Content="提交" Style="{StaticResource DefaultButton}" /> <Button x:Name="TryClose" Width="100" Margin="20" Content="取消" Style="{StaticResource DefaultButton}" /> </UniformGrid> </Grid> </StackPanel> </UserControl>
这个是含有子窗体的
下面这个没有含有子窗体
主界面
<UserControl x:Class="TrhaCabinet.Views.GoodstDataView" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:TrhaCabinet.Views.Modules.ParcelInput" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" d:DesignHeight="450" d:DesignWidth="800" mc:Ignorable="d"> <DockPanel> <GroupBox Header="{Binding DisplayName}" Style="{StaticResource MainModuleHeaderGroupBox}"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="auto" /> <RowDefinition /> <RowDefinition Height="auto" /> <RowDefinition Height="auto" /> </Grid.RowDefinitions> <!-- 日志按钮 --> <Grid> <StackPanel Orientation="Horizontal"> <Button x:Name="AddGood" Margin="10" Background="#1C86EE" Content="添加" FontSize="20" Foreground="White" Style="{StaticResource DefaultButton}" /> </StackPanel> </Grid> <Border Grid.Row="1" Background="White"> <DataGrid x:Name="MeetingDataGrid" Grid.Row="4" Margin="20,15,20,0" VerticalAlignment="Top" AlternationCount="2" AutoGenerateColumns="False" Background="LightCyan" CanUserAddRows="False" CanUserReorderColumns="False" CanUserResizeColumns="False" CanUserResizeRows="False" CanUserSortColumns="False" FontSize="16" HeadersVisibility="Column" IsReadOnly="True" RowHeaderWidth="0" SelectionMode="Single"> <DataGrid.Columns> <DataGridTextColumn Width="*" Binding="{Binding Title}" Header="序号" IsReadOnly="True" /> <DataGridTextColumn Width="*" Binding="{Binding Date, StringFormat='{}{0:yyyy-MM-dd}'}" Header="物品名称" IsReadOnly="True" /> <DataGridTextColumn Width="*" Binding="{Binding StartTime, StringFormat='{}{0:HH:mm}'}" Header="物品类型" IsReadOnly="True" /> <DataGridTextColumn Width="*" Binding="{Binding EndTime, StringFormat='{}{0:HH:mm}'}" Header="物品数量" IsReadOnly="True" /> <DataGridTextColumn Width="*" Binding="{Binding EndTime, StringFormat='{}{0:HH:mm}'}" Header="操作" IsReadOnly="True" /> </DataGrid.Columns> </DataGrid> </Border> <!-- 控制按钮 --> <Grid Grid.Row="2"> <Grid.ColumnDefinitions> <ColumnDefinition Width="100*" /> <ColumnDefinition Width="100*" /> </Grid.ColumnDefinitions> <Button Grid.Column="0" HorizontalAlignment="Right" Content="入库" Style="{StaticResource DefaultButton}" /> <Button x:Name="TryClose" Grid.Column="1" HorizontalAlignment="Right" VerticalAlignment="Bottom" Content="返回" Style="{StaticResource BackForwardButton}" /> </Grid> </Grid> </GroupBox> </DockPanel> </UserControl>
其中添加按钮绑定的还是 x:Name="AddGood"
对应的viewModel有"AddGood"
public class GoodstDataViewModel : IParcelInputModel { public GoodstDataViewModel(IEventAggregator eventAggregator) { DisplayName = "入库物品"; this.eventAggregator = eventAggregator; } private readonly IEventAggregator eventAggregator; public BindableCollection<AddGoodsViewModel> AddGoods { get; } = new BindableCollection<AddGoodsViewModel>(); public AddGoodsViewModel SelectedAddGoods { get; set; } #region 绑定 public void AddGood() { //if (AddGoods == null) return; //eventAggregator.PublishOnUIThread(new ActiveScreenInShellMsg(SelectedAddGoods)); ////if (SelectedGoods == null) return; var screen = IoC.Get<AddGoodsViewModel>(); //var screen = new GoodstDataViewModel //{ // Goods = SelectedGoods //}; eventAggregator.PublishOnUIThread(new ActiveScreenInShellMsg(screen)); } #endregion
对应的子窗体的viewmodel
public class AddGoodsViewModel : Screen { public AddGoodsViewModel(IEventAggregator eventAggregator) { this.eventAggregator = eventAggregator; } private readonly IEventAggregator eventAggregator; }
对应子窗体的Xaml
<UserControl x:Class="TrhaCabinet.Views.AddGoodsView" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:TrhaCabinet.Views.Modules.ParcelInput" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" d:DesignHeight="450" d:DesignWidth="400" mc:Ignorable="d"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="101*" /> <RowDefinition Height="101*" /> <RowDefinition Height="101*" /> </Grid.RowDefinitions> <Grid Grid.Row="0"> <StackPanel Orientation="Horizontal"> <TextBlock Margin="30,0,0,0" HorizontalAlignment="Center" VerticalAlignment="Center" Text="物品名称" /> <TextBlock Margin="100,0,0,0" HorizontalAlignment="Center" VerticalAlignment="Center" Text="物品型号" /> <TextBlock Margin="100,0,0,0" HorizontalAlignment="Center" VerticalAlignment="Center" Text="物品数量" /> </StackPanel> </Grid> <Grid Grid.Row="1"> <Grid.ColumnDefinitions> <ColumnDefinition Width="29*" /> <ColumnDefinition Width="51*" /> </Grid.ColumnDefinitions> <StackPanel Grid.ColumnSpan="2" Orientation="Horizontal"> <ComboBox Width="100" Margin="30,0,0,0" Text="物品名称" /> <ComboBox Margin="100,0,0,0" Text="物品型号" /> <Button Margin="100,0,0,0" Content="+" /> <TextBlock Text="1" /> <Button Margin="100,0,0,0" Content="-" /> </StackPanel> </Grid> <Button x:Name="TryClose" Grid.Row="2" HorizontalAlignment="Right" VerticalAlignment="Bottom" Content="返回" Style="{StaticResource BackForwardButton}" /> </Grid> </UserControl>