4

I have a datagrid say Datagrid1 that is populated with a List (Dynamically through code behind). Inside the row details template for datagrid , i want to add a datagrid, lets calls it datagrid2, the datagrid2 needs to dynamically poupulated with List on the SelectionChange event of Datagrid1 ? Accessing the datagrid2 and binding it to a datasource needs to be done in the code behind. Can somone help me out with this? my xaml is:

<Grid>
        <my:DataGrid    Name="dataGrid1" ItemsSource="{Binding}">
            <my:DataGrid.RowDetailsTemplate>
                <DataTemplate>
                    <my:DataGrid Name="dataGrid2"></my:DataGrid>
                </DataTemplate>
            </my:DataGrid.RowDetailsTemplate>
        </my:DataGrid>
    </Grid>
4

2 に答える 2

6

バインディングを介して行う方がはるかに簡単です。DetailElements のコレクションを dataGrid1 の ItemsSource の各要素に追加できます。あとは、このコレクションを dataGrid2 の ItemsSource にバインドするだけで、バインディングを通じてデータが自動的に取り込まれます。

public class DataGrid1SourceItem
{
    public ObservableCollection<DetailItem> DetailItems {get;set;}
}

XAML:

<Grid>
    <my:DataGrid    Name="dataGrid1" ItemsSource="{Binding}">
        <my:DataGrid.RowDetailsTemplate>
            <DataTemplate>
                <my:DataGrid Name="dataGrid2" ItemsSource="{Binding Path=DetailItems}"></my:DataGrid>
            </DataTemplate>
        </my:DataGrid.RowDetailsTemplate>
    </my:DataGrid>
</Grid>  

編集: DataGrid セルの値に応じてデータベースを検索するには、この値を ViewModel に取得する必要があります。これを行うには、(私の例では) プロパティを作成し、ProductNameそれを DataGridColumn の Binding プロパティ (Mode=TwoWay) にバインドします。次に、すべての製品を保持するプライベート フィールドを作成し、dataGrid2 ItemsSources コレクションでこのフィールドをフィルタリングできます。

public class DataGrid1SourceItem
{
    private List<DetailItems> _allDetailItems = new List<DetailItems>();
    public IEnumerable<DetailItem> DetailItems 
    {
       get { return _allDetailItems.Where(item => item.Name == ProductName); }
    } 

    public DataGrid1SourceItem()
    {
       // load your products into _allDetailItems
    }

    private string _productName;
    public string ProductName
    {
        get { return _productName; }
        set
        {
            _productName= value;
            OnPropertyChanged("ProductName");
            OnPropertyChanged("DetailItems");
        }
    }
}
于 2013-09-13T07:45:05.387 に答える
3

以下はあなたを助けるかもしれません

public partial class Window1 : Window
    {
        DataTable dt = new DataTable();
        public Window1()
        {
            InitializeComponent();
            dt.Columns.Add("Num1", typeof(string));
            dt.Columns.Add("Num2", typeof(string));
            dt.Rows.Add("100", "200");
            dt.Rows.Add("300", "400");
            this.dataGridTest.DataContext = dt;
            this.dataGridTest.RowDetailsVisibilityChanged += new EventHandler<Microsoft.Windows.Controls.DataGridRowDetailsEventArgs>(dataGridTest_RowDetailsVisibilityChanged);
        }
        void dataGrid1_RowDetailsVisibilityChanged(object sender, Microsoft.Windows.Controls.DataGridRowDetailsEventArgs e)
        {
            Microsoft.Windows.Controls.DataGrid  innerDataGrid = e.DetailsElement as Microsoft.Windows.Controls.DataGrid;
            innerDataGrid.ItemsSource = ((IListSource)dt).GetList();
        }
    }

XAML で

<Grid>
        <my:DataGrid  Name="dataGridTest" ItemsSource="{Binding}">
            <my:DataGrid.RowDetailsTemplate>
                <DataTemplate>
                    <my:DataGrid Name="innerGrid"></my:DataGrid>
                </DataTemplate>
            </my:DataGrid.RowDetailsTemplate>
        </my:DataGrid>
    </Grid>
于 2013-09-13T13:03:10.990 に答える