SilverLight触发器加Command命令实现一对多级联查询
先来看看SilverLight触发器加Command命令能实现什么样的功能呢
如下是两个datagrid点击上面的其中一行 下面的datagrid就会根据上面的编号 对应查出详细信息
点击第二条数据时:
好下面我们看看 怎么用触发器和Command命令来实现这样的功能
xaml代码
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<Grid x:Name="LayoutRoot" Background="White">
<Grid.RowDefinitions>
<RowDefinition Height="330*"></RowDefinition>
<RowDefinition Height="470*"></RowDefinition>
</Grid.RowDefinitions>
<Grid Grid.Row="0" >
<Grid.RowDefinitions>
<RowDefinition Height="30"></RowDefinition>
<RowDefinition Height="270*"></RowDefinition>
<RowDefinition Height="20"></RowDefinition>
</Grid.RowDefinitions>
<TextBlock FontSize="14" Margin="5" Grid.Row="0">库存总账条目:</TextBlock>
<data:DataGrid x:Name="dglist" Grid.Row="1" Margin="5,0,5,0" GridLinesVisibility="All"
AutoGenerateColumns="False"
FrozenColumnCount="1"
AreRowDetailsFrozen="True"
HorizontalAlignment="Stretch" CanUserSortColumns="False"
ItemsSource="{Binding InvStockLedgerModelList,Mode=TwoWay}"
IsReadOnly="True" SelectedItem="{Binding InvStockLedgerModel,Mode=TwoWay}">
<data:DataGrid.Columns>
<data:DataGridTextColumn Header="物料名称" Binding="{Binding MaterialModel.Mat_Name,Mode=TwoWay}"/>
<data:DataGridTextColumn Header="会计年度" Binding="{Binding StockLedger_Year,Mode=TwoWay}"/>
<data:DataGridTextColumn Header="会计期间" Binding="{Binding StockLedger_Period,Mode=TwoWay}"/>
<data:DataGridTextColumn Header="期初库存数量" Binding="{Binding StockLedger_BAmount,Mode=TwoWay}"/>
<data:DataGridTextColumn Header="期初库存金额" Binding="{Binding StockLedger_BMoney,Mode=TwoWay}" Width="100"/>
<data:DataGridTextColumn Header="收入数量" Binding="{Binding StockLedger_IAmount,Mode=TwoWay}"/>
<data:DataGridTextColumn Header="收入金额" Binding="{Binding StockLedger_IMoney,Mode=TwoWay}" Width="100"/>
<data:DataGridTextColumn Header="发出数量" Binding="{Binding StockLedger_OAmount,Mode=TwoWay}"/>
<data:DataGridTextColumn Header="发出金额" Binding="{Binding StockLedger_OMoney,Mode=TwoWay}" Width="100"/>
<data:DataGridTextColumn Header="期末数量" Binding="{Binding StockLedger_EndAmount,Mode=TwoWay}"/>
<data:DataGridTextColumn Header="期末金额" Binding="{Binding StockLedger_EndMoney,Mode=TwoWay}" Width="100"/>
</data:DataGrid.Columns>
<!--内置一个触发器
当datagrid触发SelectionChanged事件后执行Command命令
Command绑定了DataGridSelectionCommand命令
传出的对象为datagrid的SelectedItem
数据来源于dglist和datagrid的名字相同-->
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<i:InvokeCommandAction Command="{Binding DataGridSelectionCommand}" CommandParameter="{Binding SelectedItem,ElementName=dglist}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</data:DataGrid>
<Border Grid.Row="2" BorderThickness="1,0,1,1" BorderBrush="#6c7175" Margin="5,0,5,0" >
<Grid x:Name="gridFooter" HorizontalAlignment="Stretch" VerticalAlignment="Top" >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<sdk:Label Content="合 计:" FontSize="13" />
<sdk:Label Grid.Column="3" FontSize="13" BorderThickness="1,0,0,0" BorderBrush="#6c7175" x:Name="lblStockLedger_BAmount" />
<sdk:Label Grid.Column="4" FontSize="13" BorderThickness="1,0,0,0" BorderBrush="#6c7175" x:Name="lblStockLedger_BMoney" />
<sdk:Label Grid.Column="5" FontSize="13" BorderThickness="1,0,0,0" BorderBrush="#6c7175" x:Name="lblStockLedger_IAmount" />
<sdk:Label Grid.Column="6" FontSize="13" BorderThickness="1,0,0,0" BorderBrush="#6c7175" x:Name="lblStockLedger_IMoney" />
<sdk:Label Grid.Column="7" FontSize="13" BorderThickness="1,0,0,0" BorderBrush="#6c7175" x:Name="lblStockLedger_OAmount"/>
<sdk:Label Grid.Column="8" FontSize="13" BorderThickness="1,0,0,0" BorderBrush="#6c7175" x:Name="lblStockLedger_OMoney" />
<sdk:Label Grid.Column="9" FontSize="13" BorderThickness="1,0,0,0" BorderBrush="#6c7175" x:Name="lblStockLedger_EndAmount"/>
<sdk:Label Grid.Column="10" FontSize="13" BorderThickness="1,0,0,0" BorderBrush="#6c7175" x:Name="lblStockLedger_EndMoney"/>
</Grid>
</Border>
</Grid>
<Grid Grid.Row="1" >
<Grid.RowDefinitions>
<RowDefinition Height="30"></RowDefinition>
<RowDefinition Height="410*"></RowDefinition>
<RowDefinition Height="20"></RowDefinition>
</Grid.RowDefinitions>
<TextBlock FontSize="14" Margin="5" Grid.Row="0">总账明细:</TextBlock>
<data:DataGrid x:Name="gridList" Grid.Row="1" Margin="5,0,5,0" GridLinesVisibility="All"
AutoGenerateColumns="False"
FrozenColumnCount="1"
AreRowDetailsFrozen="True"
HorizontalAlignment="Stretch" CanUserSortColumns="False"
ItemsSource="{Binding InvStockListModelList,Mode=TwoWay}"
IsReadOnly="True" SelectedItem="{Binding InvStockListModel,Mode=TwoWay}">
<data:DataGrid.Columns>
<data:DataGridTextColumn Header="物料名称" Binding="{Binding MaterialModel.Mat_Name,Mode=TwoWay}"/>
<data:DataGridTextColumn Header="仓库名称" Binding="{Binding StorgeModel.Storge_Name,Mode=TwoWay}"/>
<data:DataGridTextColumn Header="库位名称" Binding="{Binding InvLocationModel.Location_Name,Mode=TwoWay}"/>
<data:DataGridTextColumn Header="批号" Binding="{Binding StockList_Batch,Mode=TwoWay}"/>
<data:DataGridTextColumn Header="年度" Binding="{Binding StockList_Year,Mode=TwoWay}"/>
<data:DataGridTextColumn Header="会计期间" Binding="{Binding StockList_Period,Mode=TwoWay}"/>
<data:DataGridTextColumn Header="收入数量" Binding="{Binding StockList_IAmount,Mode=TwoWay}"/>
<data:DataGridTextColumn Header="收入单价" Binding="{Binding StockList_IPrice,Mode=TwoWay}"/>
<data:DataGridTextColumn Header="收入金额" Binding="{Binding StockList_IMoney,Mode=TwoWay}" Width="100"/>
<data:DataGridTextColumn Header="发出数量" Binding="{Binding StockList_OAmount,Mode=TwoWay}"/>
<data:DataGridTextColumn Header="发出单价" Binding="{Binding StockList_OPrice,Mode=TwoWay}"/>
<data:DataGridTextColumn Header="发出金额" Binding="{Binding StockList_OMoney,Mode=TwoWay}" Width="100"/>
<data:DataGridTextColumn Header="期末数量" Binding="{Binding StockList_EndAmount,Mode=TwoWay}"/>
<data:DataGridTextColumn Header="期末金额" Binding="{Binding StockList_EndMoney,Mode=TwoWay}" Width="100"/>
<data:DataGridTextColumn Header="供应商批号" Binding="{Binding StockList_SuppBatch,Mode=TwoWay}"/>
</data:DataGrid.Columns>
</data:DataGrid>
<Border Grid.Row="2" BorderThickness="1,0,1,1" BorderBrush="#6c7175" Margin="5,0,5,0" >
<Grid x:Name="gridFooterList" HorizontalAlignment="Stretch" Grid.Row="2" VerticalAlignment="Top" Margin="5,0,5,0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<sdk:Label Content="合 计:" FontSize="13" />
<sdk:Label Grid.Column="6" FontSize="13" BorderThickness="1,0,0,0" BorderBrush="#6c7175" x:Name="lblStockList_IAmount" />
<sdk:Label Grid.Column="8" FontSize="13" BorderThickness="1,0,0,0" BorderBrush="#6c7175" x:Name="lblStockList_IMoney" />
<sdk:Label Grid.Column="9" FontSize="13" BorderThickness="1,0,0,0" BorderBrush="#6c7175" x:Name="lblStockList_OAmount" />
<sdk:Label Grid.Column="11" FontSize="13" BorderThickness="1,0,0,0" BorderBrush="#6c7175" x:Name="lblStockList_OMoney" />
<sdk:Label Grid.Column="12" FontSize="13" BorderThickness="1,0,0,0" BorderBrush="#6c7175" x:Name="lblStockList_EndAmount" />
<sdk:Label Grid.Column="13" FontSize="13" BorderThickness="1,0,0,0" BorderBrush="#6c7175" x:Name="lblStockList_EndMoney" />
</Grid>
</Border>
</Grid>
</Grid>
cs代码:
注意:代码里面的属性列表集合及实体对象都要继承于INotifyPropertyChanged
当属性改变事要通知到界面上来。。。。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
//声明Command命令
public ICommand DataGridSelectionCommand { protected set; get; }
//在构造函数里面Command命令执行
DataGridSelectionCommand = new DelegateCommand(DoDataGridSelectionCommand);
//实现Command命令
void DoDataGridSelectionCommand(object param)
{
if (param == null) return;
InvStockLedgerModel = (InvStockLedgerModel)param;
//根据界面数据 从数据库中详细表中的数据
InvStockListModelList =GetPostData<IList<InvStockListModel>>(InvStockLedgerModel);
GetStockListSum();
大功告成!!!