私は、(RowDetailsVisibilityChangedを使用して)データグリッドをクリックするとうまく動作するネストされたグリッドを持っています。 その後、私はエキスパンダーを挿入しましたが、今は問題をすべて解決しています。問題はエキスパンダーが(オブジェクト送信者、RoutedEventArgs e)、rowdetails(オブジェクト送信者、DataGridRowDetailsEventArgs e) 。私のエキスパンダーは拡大して動作しますが、ネストされたグリッドにはデータを入力しません。 Expandedメソッドから何とかRowDetailsVisibilityChangedを呼び出すことはできますか?datagrid expander + 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;
}
}
}
マイ: 主な問題は、私は
DataGrid innerDataGrid = e.DetailsElement as DataGrid;
を定義しなければならないと私は私が
が拡大DataGridRowDetailsEventArgsを持っていないため、拡張メソッドでそれを行うとメソッドをRowDetailsVisibilityChangedことができないということです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