将内部数据网格数据与外部数据网格wpf绑定

时间:2014-03-17 20:12:03

标签: c# wpf datagrid rowdetailstemplate

我有一个源数据网格,它绑定一个名为source的数据表和一个内部数据网格(可以通过rowdetails点击一个按钮来查看),它绑定了一个名为inner的不同数据表。这两个属性都是从数据库中从2个不同的表中填充的,然而" TradeID"是两个表之间的共同字段。我只希望看到对应于" TraderID"的行。当我最大化我的rowdetails。这是我到目前为止的c#:

try
        {
            SourceTable = new DataTable();
            InnerTable = new DataTable();

            SourceTable.Columns.AddRange(new DataColumn[]{
                new DataColumn("InputID", typeof(int)),
                new DataColumn("TraderID", typeof(string)),
                new DataColumn("TradeDate", typeof(DateTime)),
                new DataColumn("TradeTime", typeof(TimeSpan)),
                new DataColumn("ClientName", typeof(string)),
                new DataColumn("CurPair", typeof(string)),
                new DataColumn("Amnt", typeof(int)),
                new DataColumn("Action", typeof(string)),
                new DataColumn("ExecutedRate", typeof(decimal))
            });

            InnerTable.Columns.AddRange(new DataColumn[]{
                new DataColumn("TraderID", typeof(string)),
                new DataColumn("TradeDate", typeof(DateTime)),
                new DataColumn("TradeTime", typeof(TimeSpan)),
                new DataColumn("Amnt", typeof(int)),
                new DataColumn("Action", typeof(string)),
                new DataColumn("ExecutedRate", typeof(decimal)),
                new DataColumn("Type", typeof(string))
            });

            DataRow rowSource = null;
            DataRow rowInner = null;

            var OpenTradesQuery = from qa in connection.QuickAnalyzerInputs
                                  where qa.TradeClosedDateTime == null
                                  select new
                                  {
                                      qa.InputID,
                                      qa.TraderID,
                                      qa.ClientTradedDate,
                                      qa.ClientTradedTime,
                                      qa.ClientName,
                                      qa.CurrencyPair,
                                      qa.TradedAmount,
                                      qa.Action,
                                      qa.ExecutedRate
                                  };


            var ChildTradesQuery = from ct in connection.ChildTradeDetails
                                  join qa in connection.QuickAnalyzerInputs on ct.TradeID equals qa.InputID                                         
                                  where qa.TradeClosedDateTime == null
                                  select new
                                  {
                                      ct.TraderID,
                                      ct.ClientTradedDate,
                                      ct.ClientTradedTime,
                                      ct.TradedAmount,
                                      ct.Action,
                                      ct.ExecutedRate,
                                      ct.Type
                                  };

            if (OpenTradesQuery.Count() > 0)
            {
                numOfrecords = OpenTradesQuery.Count();
                DataContext = this;

                foreach (var rowObj in OpenTradesQuery)
                {
                    rowSource = SourceTable.NewRow();
                    SourceTable.Rows.Add(rowObj.InputID, rowObj.TraderID, rowObj.ClientTradedDate, rowObj.ClientTradedTime, rowObj.ClientName, rowObj.CurrencyPair, rowObj.TradedAmount, rowObj.Action, rowObj.ExecutedRate);
                }

                foreach (var rowObj in ChildTradesQuery)
                {
                    rowInner = SourceTable.NewRow();
                    InnerTable.Rows.Add(rowObj.TraderID, rowObj.ClientTradedDate, rowObj.ClientTradedTime, rowObj.TradedAmount, rowObj.Action, rowObj.ExecutedRate, rowObj.Type);
                }

                this.DG_openTrades.RowDetailsVisibilityChanged += new EventHandler<ExtendedGrid.Microsoft.Windows.Controls.DataGridRowDetailsEventArgs>(dataGrid_RowDetailsVisibilityChanged);
            }


private void dataGrid_RowDetailsVisibilityChanged(object sender, ExtendedGrid.Microsoft.Windows.Controls.DataGridRowDetailsEventArgs e)
{
        ExtendedGrid.Microsoft.Windows.Controls.DataGrid innerDataGrid = e.DetailsElement as ExtendedGrid.Microsoft.Windows.Controls.DataGrid;

        innerDataGrid.ItemsSource = ((IListSource)InnerTable).GetList();
}

一些XAML:

<ExtendedGridControl:ExtendedDataGrid x:Name="DG_openTrades"
                                          RowHeaderWidth="17"  AutoGenerateColumns="False" HideColumnChooser="False" 
                                          ItemsSource="{Binding SourceTable}"
                                          IsSynchronizedWithCurrentItem="True"
                                          HorizontalScrollBarVisibility="Visible"
                                          VerticalScrollBarVisibility="Visible"
                                          SelectionMode="Extended" 
                                          SelectionUnit="FullRow"
                                          CanUserAddRows="False"
                                          CanUserDeleteRows="False"
                                          SearchAutoFilterVisibility="Visible" AutoFilterFontSize="10"
                                          Grid.ColumnSpan="3" Grid.Column="1" Grid.Row="2" Margin="0,0,0,1" 
                                          RowDetailsVisibilityMode="Collapsed"
                                          >
        <ExtendedGridControl:ExtendedDataGrid.RowDetailsTemplate>
            <DataTemplate>
                <ExtendedGridControl:ExtendedDataGrid Name="innerDataGrid" 
                                                      HeadersVisibility="Row" 
                                                      GroupByControlVisibility="Collapsed">                        
                </ExtendedGridControl:ExtendedDataGrid>
            </DataTemplate>
        </ExtendedGridControl:ExtendedDataGrid.RowDetailsTemplate>

        <Control:DataGrid.ContextMenu>
            <ContextMenu >
                <MenuItem Click="IncTradeAmnt_Click" Header="Increase">
                    <MenuItem.Icon>
                        <Image Width="15" Height="15" Source="../plus-26.png"/>
                    </MenuItem.Icon>
                </MenuItem>

                <MenuItem Click="DecTradeAmnt_Click" Header="Decrease">
                    <MenuItem.Icon>
                        <Image Width="15" Height="15" Source="../Images/minus-26.png"/>
                    </MenuItem.Icon>
                </MenuItem>

                <MenuItem Click="CloseTrade_Click" Header="Close">
                    <MenuItem.Icon>
                        <Image Width="15" Height="15" Source="../Images/close_window-26.png"/>
                    </MenuItem.Icon>
                </MenuItem>
            </ContextMenu>
        </Control:DataGrid.ContextMenu>

        <Control:DataGrid.Columns>

            <Control:DataGridTemplateColumn>
                <Control:DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <Button Click="ShowHideDetails" Foreground="Black">+</Button>
                    </DataTemplate>
                </Control:DataGridTemplateColumn.CellTemplate>
            </Control:DataGridTemplateColumn>

            <ExtendedColumn:ExtendedDataGridTextColumn Header="ID" AllowAutoFilter="True"
                                       Binding="{Binding InputID}" IsReadOnly="True" CanUserSort="True"/>

现在当我最大化我的视图时,我只看到从ChildTradeDetails表中查询的所有内容,如何查询该数据表以仅显示与我选择的该行中的tradeId对应的数据?

修改

如何获取RowDetailsVisibilityChanged事件中所选行的第一列的值?

1 个答案:

答案 0 :(得分:0)

从内存中我告诉你,你可以像这样绑定所选项目: SelectedItem="{binding selectedRow, mode=twoway}"

如果值可以更改,最好是为observableCollection<YourRowModelType>中的每一行使用模型,否则使用IEnumerable<YourRowModelType>

以此为例:

WPF Binding SelectedItem in DataGrid

然后,您将能够在每个事件中访问所选行...

这是一个mvvm模式...

希望有所帮助