如何将分层数据绑定到WPF网格 [英] How To Bind Hierarchical Data to WPF grid(s)

查看:98
本文介绍了如何将分层数据绑定到WPF网格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



我在MSDN论坛上开始讨论如何从头开发一个具有嵌套的酷外观第三方数据网格相关数据的网格。在这一点上,我只是寻求帮助,以使数据显示更少进入花哨的树状选择列。完整的主题可以在这里查看:



http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/fd88c680-02ce-4e5d-9588-60cb380ddd85 [<一个href =target =_ blank> ]



我的问题简称为#1。我的XAML是否使用了行详细信息模板正确的路径。



Hello,

I started a thread over on a MSDN forum about how to develop from scratch one of those "cool" looking 3rd party datagrids that have a nested grid for related data. At this point I'm just looking for help to get the data to show much less get into the fancy "tree like" selection column. The full thread can be reviewed here:

http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/fd88c680-02ce-4e5d-9588-60cb380ddd85[]

My questions in short are #1. Is my XAML use of the row details template the correct path.

<DataGrid IsReadOnly="False" AutoGenerateColumns="False" x:Name="dg_SmeAFe">
    <DataGrid.Columns>
        <DataGridCheckBoxColumn Header="AFE Select" IsReadOnly="False" />
        <DataGridTextColumn Binding="{Binding Path=AfeId}" Header="AFE ID" />
        <DataGridTextColumn Binding="{Binding Path=AfeStatusName}" Header="Afe Status" />
        <DataGridTextColumn Binding="{Binding Path=CustName}" Header="Customer Name" />
        <DataGridTextColumn Binding="{Binding Path=RegionName}" Header="Region Name" />
        <DataGridTextColumn Binding="{Binding Path=PropertyNumber}" Header="Property Number" />
    </DataGrid.Columns>

    <DataGrid.RowDetailsTemplate>
        <DataTemplate>
            <DataGrid AutoGenerateColumns="False" x:Name="dgChld_SmeTasks" IsReadOnly="False" ItemsSource="{Binding}">
                <DataGridCheckBoxColumn Header="Task Select" IsReadOnly="False" />
                <DataGridTextColumn Binding="{Binding Path=DisplayName}" Header="Work to Perform" />
                <DataGridTextColumn Binding="{Binding Path=Status}" Header="Status" />
            </DataGrid>
        </DataTemplate>
    </DataGrid.RowDetailsTemplate>

</DataGrid>



和#2。如何将数据绑定到表格并告诉他们他们之间的关系?

下面的代码是伪代码,以显示我认为我们的方法。




and #2. how do I bind the data to the tables and tell them about their relationship?
The below code is pseudo code to show what I thought we be the approach.

//Bind parent table(Afes) in Dataset(ds_AfeTaska) to Parent grid
AfeDg.ItemsSource = ds_AfeTaska.Tables["Afes"].DefaultView;

//Bind details table(APTasks) in Dataset(ds_AfeTaska) to details grid
AfeChilDg.ItemsSource = ds_AfeTaska.Tables["APTasks"].DefaultView;

//Inform parent there is a relation *Remember this is a bogus property*
AfeDg.IsRelationTable = true;

//Inform parent what column data provides the lookup key for the details grid row selection
AfeDg.RelationKeyColumn = "AfeID";





是的,我已经在AfeID列的两个表之间的DataSet中定义了关系。这似乎是微不足道的,但没有得到谷歌的答案...虽然我学到了一些不相关的酷编程的东西。



无论如何,任何帮助都将不胜感激。

JB



And yes I do already have a relationship defined in the DataSet between the two tables on the AfeID column. This seems so trivial yet not getting the answers from Google...although I learned some unrelated cool programming things.

Anyway any help would be greatly appreciated.
JB

推荐答案

private void gItemInfo1_LoadingRowDetails(object sender,DataGridRowDetailsEventArgs e)

{

DataGridTextColumn列;

int SelectedIndex;

绑定bItems;

尝试

{

if(((System.Windows.Controls.Panel)(e.DetailsElement))。Children.Count> 0)

{

SelectedIndex =((DataGrid) (发送者))。SelectedIndex;

DataGrid MyDetailsGrid =(DataGrid)e.DetailsElement.FindName(dgChld_SmeTasks);

MyDetailsGrid.Columns.Clear();



Column = new Dat aGridTextColumn();

Column.Header =ID;

bItems = new Binding(IID);

bItems.Mode = BindingMode.TwoWay;

Column.Binding = bItems;

Column.Width = new DataGridLength(154,DataGridLengthUnitType.Star);



MyDetailsGrid.Columns.Add(Column);



Column = new DataGridTextColumn();

Column.Header = 名称;

bItems = new Binding(SItemName);

bItems.Mode = BindingMode.TwoWay;

Column.Binding = bItems;

Column.Width = new DataGridLength(155,DataGridLengthUnitType.Star);

MyDetailsGrid.Columns.Add(Column);



Column = new DataGri dTextColumn();

Column.Header =Price;

bItems = new Binding(IPrice);

bItems.Mode = BindingMode.TwoWay;

Column.Binding = bItems;

Column.Width = new DataGridLength(154,DataGridLengthUnitType.Star);

MyDetailsGrid。 Columns.Add(Column);



Column = new DataGridTextColumn();

Column.Header =Postal Code;

bItems = new Binding(IPostalCode);

bItems.Mode = BindingMode.TwoWay;

Column.Binding = bItems;

Column.Width = new DataGridLength(155,DataGridLengthUnitType.Star);

MyDetailsGrid.Columns.Add(Column);

MyDetailsGrid.Width = MyDetailsGrid.Width - 20;

列表< itemsmapping> objItemMap = new List< itemsmapping> {(ItemsMapping)dg_SmeAFe.Items [SelectedIndex]};

MyDetailsGrid.ItemsSource =(List< itemsdetails>)objItemMap [0] .ICollectionItemDetails;

}

}

catch(例外)

{

// throw;

}

}



关于LoadingRowDetails的一些事情
private void gItemInfo1_LoadingRowDetails(object sender, DataGridRowDetailsEventArgs e)
{
DataGridTextColumn Column;
int SelectedIndex;
Binding bItems;
try
{
if (((System.Windows.Controls.Panel)(e.DetailsElement)).Children.Count > 0)
{
SelectedIndex = ((DataGrid)(sender)).SelectedIndex;
DataGrid MyDetailsGrid = (DataGrid)e.DetailsElement.FindName("dgChld_SmeTasks");
MyDetailsGrid.Columns.Clear();

Column = new DataGridTextColumn();
Column.Header = "ID";
bItems = new Binding("IID");
bItems.Mode = BindingMode.TwoWay;
Column.Binding = bItems;
Column.Width = new DataGridLength(154, DataGridLengthUnitType.Star);

MyDetailsGrid.Columns.Add(Column);

Column = new DataGridTextColumn();
Column.Header = "Name";
bItems = new Binding("SItemName");
bItems.Mode = BindingMode.TwoWay;
Column.Binding = bItems;
Column.Width = new DataGridLength(155, DataGridLengthUnitType.Star);
MyDetailsGrid.Columns.Add(Column);

Column = new DataGridTextColumn();
Column.Header = "Price";
bItems = new Binding("IPrice");
bItems.Mode = BindingMode.TwoWay;
Column.Binding = bItems;
Column.Width = new DataGridLength(154, DataGridLengthUnitType.Star);
MyDetailsGrid.Columns.Add(Column);

Column = new DataGridTextColumn();
Column.Header = "Postal Code";
bItems = new Binding("IPostalCode");
bItems.Mode = BindingMode.TwoWay;
Column.Binding = bItems;
Column.Width = new DataGridLength(155, DataGridLengthUnitType.Star);
MyDetailsGrid.Columns.Add(Column);
MyDetailsGrid.Width = MyDetailsGrid.Width - 20;
List<itemsmapping> objItemMap = new List<itemsmapping> { (ItemsMapping)dg_SmeAFe.Items[SelectedIndex] };
MyDetailsGrid.ItemsSource = (List<itemsdetails>)objItemMap[0].ICollectionItemDetails;
}
}
catch (Exception)
{
// throw;
}
}

Some thing like this on "LoadingRowDetails"


嘿,你必须在DetialsRowTemplate中定义行像这样:



Hey you must define the rows in the DetialsRowTemplate like this:

<datagrid.rowdetailstemplate>
        <datatemplate>
            <datagrid autogeneratecolumns="False" x:name="dgChld_SmeTasks" isreadonly="False" itemssource="{Binding}" xmlns:x="#unknown">
              <datagrid.columns>
                <datagridcheckboxcolumn header="Task Select" isreadonly="False" />
                <datagridtextcolumn binding="{Binding Path=DisplayName}" header="Work to Perform" />
                <datagridtextcolumn binding="{Binding Path=Status}" header="Status" />
              </datagrid.columns>
            </datagrid>
        </datatemplate>
    </datagrid.rowdetailstemplate>


这篇关于如何将分层数据绑定到WPF网格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆