0
我有一個嵌套的網格,當我點擊一個數據網格(使用RowDetailsVisibilityChanged)時,工作正常。 之後,我插入了一個擴展器,現在我有問題把它放在一起。問題是擴展器擴展時調用的方法有(對象發件人,RoutedEventArgs e)和rowdetails有(對象發件人,DataGridRowDetailsEventArgs e) 。我的擴展器適用於擴展,但不填充嵌套網格中的數據。 我可以以某種方式從Expanded方法調用RowDetailsVisibilityChanged嗎? 的主要問題是,我必須定義datagrid擴展器+ RowDetailsVisibilityChanged
DataGrid innerDataGrid = e.DetailsElement as DataGrid;
,我不能這樣做,在展開的方法,因爲我沒有DataGridRowDetailsEventArgs
擴大,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