datagrid expander + RowDetailsVisibilityChanged

时间:2017-07-18 13:16:45

标签: c# wpf datagrid wpfdatagrid

我有一个嵌套网格,当我点击一个数据网格(使用RowDetailsVisibilityChanged)时,它可以正常工作。 之后我插入了一个扩展器,现在我遇到了将它们放在一起的问题。问题是展开时扩展器调用的方法是(object sender,RoutedEventArgs e),rowdetails有(object sender,DataGridRowDetailsEventArgs e)。我的扩展器可以扩展,但不会在嵌套网格中填充数据。 我可以以某种方式从Expanded方法调用RowDetailsVisibilityChanged吗? 主要问题是我必须定义

DataGrid innerDataGrid = e.DetailsElement as DataGrid;

我不能在扩展方法中这样做,因为我没有DataGridRowDetailsEventArgs

Expanded和RowDetailsVisibilityChanged方法:

private void tabela_RowDetailsVisibilityChanged(object sender, DataGridRowDetailsEventArgs e)
{
    DataGrid innerDataGrid = e.DetailsElement as DataGrid;
    string ConString = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
    string query = "SELECT StanjeTR.Firma, StanjeTR.NazFirme, StanjeTR.Konto, StanjeTR.NazivKonta, Sum(StanjeTR.Duguje) AS SDuguje, Sum(StanjeTR.Potrazuje) AS SPotrazuje, Sum(StanjeTR.Saldo) AS Stanje, StanjeTR.Valuta, Sum(StanjeTR.DevDuguje) AS DDuguje, Sum(StanjeTR.DevPotrazuje) AS DPotrazuje, Sum(StanjeTR.DevSaldo) AS StanjeDeviza FROM StanjeTR WHERE StanjeTr.Firma = @Firma GROUP BY StanjeTR.Firma, StanjeTR.NazFirme, StanjeTR.Konto, StanjeTR.NazivKonta, StanjeTR.Valuta ORDER BY StanjeTR.Firma, StanjeTR.Konto";

    using (SqlConnection con = new SqlConnection(ConString))
    {
        //CmdString = "SELECT emp_id, fname, lname, hire_date FROM Employee";
        SqlCommand cmd = new SqlCommand(query, con);
        SqlDataAdapter sda = new SqlDataAdapter(cmd);
        cmd.Parameters.Add("@Firma", SqlDbType.Int).Value = Convert.ToInt32(izborID);
        DataTable dt = new DataTable("StanjeTR");
        sda.Fill(dt);
        innerDataGrid.ItemsSource = dt.DefaultView;     
    }
}

private void Expander_Expanded(object sender, RoutedEventArgs e)
{   
    for (var vis = sender as Visual; vis != null; vis = VisualTreeHelper.GetParent(vis) as Visual)
    {
        if (vis is DataGridRow)
        {
            var row = (DataGridRow)vis;
            row.DetailsVisibility = row.DetailsVisibility == Visibility.Visible ? Visibility.Collapsed : Visibility.Visible;
            break;
        }
    }
}

private void Expander_Collapsed(object sender, RoutedEventArgs e)
{
    for (var vis = sender as Visual; vis != null; vis = VisualTreeHelper.GetParent(vis) as Visual)
    {
        if (vis is DataGridRow)
        {
            var row = (DataGridRow)vis;
            row.DetailsVisibility = row.DetailsVisibility == Visibility.Visible ? Visibility.Collapsed : Visibility.Visible;
            break;
        }
    }
}

我的XAML:

<DataGrid Name="tabela" ItemsSource="{Binding}" RowDetailsVisibilityChanged="tabela_RowDetailsVisibilityChanged" AutoGenerateColumns="False" SelectionChanged="tabela_SelectionChanged">
    <DataGrid.CellStyle>
        <Style TargetType="DataGridCell">
            <Setter Property="BorderThickness" Value="0"/>
        </Style>
    </DataGrid.CellStyle>

    <DataGrid.RowHeaderTemplate>
        <DataTemplate>
            <Expander Expanded="Expander_Expanded" Collapsed="Expander_Collapsed" />
        </DataTemplate>
    </DataGrid.RowHeaderTemplate>

    <DataGrid.RowDetailsTemplate>
        <DataTemplate>  
            <DataGrid Name="innerDataGrid" AutoGenerateColumns="False">
                <DataGrid.Columns>
                    <DataGridTextColumn Header="Konto" Binding="{Binding Konto}" Width="200" />
                    <DataGridTextColumn Header="Naziv Konta" Binding="{Binding NazivKonta}" Width="200" />
                    <DataGridTextColumn Header="SDuguje" Binding="{Binding SDuguje}" Width="200" />
                    <DataGridTextColumn Header="SPotrazuje" Binding="{Binding SPotrazuje}" Width="200" />
                </DataGrid.Columns>
            </DataGrid>
        </DataTemplate>
    </DataGrid.RowDetailsTemplate>

    <DataGrid.Columns>
        <DataGridTextColumn Binding="{Binding Firma}" Width="50" Header="Firma"/>
        <DataGridTextColumn Binding="{Binding NazFirme}" Width="200" Header="Naziv Firme"/>
        <DataGridTextColumn Binding="{Binding SDuguje}" Width="200" Header="Duguje"/>
        <DataGridTextColumn Binding="{Binding SPotrazuje}" Width="200" Header="Potrazuje"/>
        <DataGridTextColumn Binding="{Binding Valuta}" Width="100" Header="Valuta"/>
        <DataGridTextColumn Binding="{Binding DDuguje}" Width="200" Header="Dev Duguje"/>
        <DataGridTextColumn Binding="{Binding DPotrazuje}" Width="200" Header="Dev Potrazuje"/>
        <DataGridTextColumn Binding="{Binding StanjeDeviza}" Width="*" Header="Stanje deviza"/>
    </DataGrid.Columns>

</DataGrid>code here

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。 我将我的扩展器添加为DataGridTemplateColumn,并且在扩展器的展开和折叠事件中添加了Visibility attirbutes。