将动态数据绑定到 rowdetails 模板内的 wpf 数据网格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18779960/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:38:41  来源:igfitidea点击:

Bind dynamic data to wpf datagrid inside rowdetails template

c#wpfxamldatagridwpftoolkit

提问by Mussammil

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:

我有一个数据网格,比如 Datagrid1,它填充了一个列表(通过背后的代码动态地)。在 datagrid 的行详细信息模板中,我想添加一个 datagrid,我们称之为 datagrid2,datagrid2 需要在 Datagrid1 的 SelectionChange 事件上动态填充 List 吗?访问 datagrid2 并将其绑定到数据源需要在后面的代码中完成。有人可以帮我解决这个问题吗?我的 xaml 是:

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

回答by Florian Gl

It would be much easier to do it via bindings. You could add a collection of DetailElements to each Element of your dataGrid1's ItemsSource. Now all you have to do is binding this collection to your dataGrid2' ItemsSource and it gets automatically populated with data through the binding.

通过绑定来做到这一点会容易得多。您可以将 DetailElements 集合添加到 dataGrid1 的 ItemsSource 的每个 Element。现在您所要做的就是将此集合绑定到您的 dataGrid2 的 ItemsSource,它会通过绑定自动填充数据。

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

The XAML:

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>  

EDIT: To search your database depending on the DataGrid cells value, you have to get this value into your ViewModel. To do this create a property (in my example ProductName) and bind it to the DataGridColumn's Binding property (Mode=TwoWay). Then you could have a private field which holds all products and filter this field in the dataGrid2 ItemsSources collection:

编辑:要根据 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");
        }
    }
}

回答by Debashrita

Following may help you

以下可能对你有帮助

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();
        }
    }

In XAML

在 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>