【问题标题】:Bind dynamic data to wpf datagrid inside rowdetails template将动态数据绑定到 rowdetails 模板内的 wpf datagrid
【发布时间】:2013-09-17 18:23:24
【问题描述】:

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

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

【问题讨论】:

    标签: c# wpf xaml datagrid wpftoolkit


    【解决方案1】:

    通过绑定来实现会容易得多。您可以将 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");
            }
        }
    }
    

    【讨论】:

    • 当dataGrid1单元格文本发生变化时,我想用数据库中的一些产品填充dataGrid2(比如'select ProductName from TblProducts where ProductName Like '%"+Datagrid1.cell.text+"%' " ) 先生,该怎么做?
    【解决方案2】:

    以下内容可能对您有所帮助

    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>
    

    【讨论】:

    • ok了,当从innergrid中选中一个item时,我想把item映射到OuterGrid的对应单元格中,并且将选中的item赋值给outergrid的cell后必须使innergrid不可见如何实现这个?
    猜你喜欢
    • 2016-11-05
    • 2013-02-28
    • 2011-10-17
    • 1970-01-01
    • 2021-12-27
    • 1970-01-01
    • 2011-04-13
    • 2017-10-13
    • 1970-01-01
    相关资源
    最近更新 更多