c#数据绑定之数据转化为信息的示例
目标界面:
xaml代码:
<grid margin="2">
<grid.rowdefinitions>
<rowdefinition height="auto"/>
<rowdefinition height="auto"/>
<rowdefinition height="auto"/>
<rowdefinition />
</grid.rowdefinitions>
<groupbox header="customer" grid.row="0" padding="5">
<grid>
<grid.rowdefinitions>
<rowdefinition />
<rowdefinition />
<rowdefinition/>
</grid.rowdefinitions>
<grid.columndefinitions>
<columndefinition width="auto"/>
<columndefinition/>
</grid.columndefinitions>
<textblock grid.row="0" grid.column="0" text="customerid" margin="2" horizontalalignment="right" verticalalignment="center"/>
<textbox grid.row="0" grid.column="1" margin="2" name="tbxcustomerid" text="{binding path=cid}"/>
<textblock grid.row="1" grid.column="0" text="customername" margin="2" horizontalalignment="right" verticalalignment="center"/>
<textbox grid.row="1" grid.column="1" margin="2" name="tbxcustomername" text="{binding path=name}"/>
<button grid.row="2" grid.column="1" content="add new customer" margin="2" name="btnaddcustomer" padding="2" click="btnaddcustomer_click" />
</grid>
</groupbox>
<groupbox header="order" grid.row="1" padding="5">
<grid>
<grid.rowdefinitions>
<rowdefinition />
<rowdefinition />
<rowdefinition />
<rowdefinition />
<rowdefinition />
</grid.rowdefinitions>
<grid.columndefinitions>
<columndefinition width="auto"/>
<columndefinition/>
</grid.columndefinitions>
<textblock grid.row="0" grid.column="0" text="orderid" margin="2" horizontalalignment="right" verticalalignment="center"/>
<textbox grid.row="0" grid.column="1" margin="2" name="tbxorderid" text="{binding path=oid}"/>
<textblock grid.row="1" grid.column="0" text="ordername" margin="2" horizontalalignment="right" verticalalignment="center"/>
<textbox grid.row="1" grid.column="1" margin="2" name="tbxordername" text="{binding path=customer}"/>
<textblock grid.row="2" grid.column="0" text="subtotal" margin="2" horizontalalignment="right" verticalalignment="center"/>
<textbox grid.row="2" grid.column="1" margin="2" name="tbxsubtotal" text="{binding path=subtotal}"/>
<textblock grid.row="3" grid.column="0" text="taxrate" margin="2" horizontalalignment="right" verticalalignment="center"/>
<textbox grid.row="3" grid.column="1" margin="2" name="tbxtaxrate" text="{binding path=taxrate}"/>
<button grid.row="4" grid.column="1" content="add new order" margin="2" name="btnaddorder" padding="2" click="btnaddorder_click" />
</grid>
</groupbox>
<listview name="lstdisplaycustomer" itemssource="{binding}" grid.row="2" margin="2" minheight="150">
<listview.view>
<gridview>
<gridview.columns>
<gridviewcolumn header="customerid" displaymemberbinding="{binding cid}"/>
<gridviewcolumn header="customername" displaymemberbinding="{binding name}"/>
<gridviewcolumn header="total" displaymemberbinding="{binding ordertotals}" />
</gridview.columns>
</gridview>
</listview.view>
</listview>
<listview name="lstdisplayorder" itemssource="{binding}" grid.row="3" margin="2" minheight="150">
<listview.view>
<gridview>
<gridview.columns>
<gridviewcolumn header="orderid" displaymemberbinding="{binding oid}"/>
<gridviewcolumn header="customer" displaymemberbinding="{binding customer}"/>
<gridviewcolumn header="subtotal" displaymemberbinding="{binding subtotal}"/>
<gridviewcolumn header="taxrate" displaymemberbinding="{binding taxrate}" />
<gridviewcolumn header="total" displaymemberbinding="{binding total}"/>
</gridview.columns>
</gridview>
</listview.view>
</listview>
</grid>
c# 代码:
dataset business = newdata();
public mainwindow()
{
initializecomponent();
}
private static dataset newdata()
{
//-----build the parent table and add some data
datatable customer = new datatable("customer");
customer.columns.add("cid", typeof(int32));
customer.columns.add("name", typeof(string));
//-------build the child table and add some data.
datatable orders = new datatable("order");
orders.columns.add("oid", typeof(int));
orders.columns.add("customer", typeof(int32));
orders.columns.add("subtotal", typeof(decimal));
orders.columns.add("taxrate", typeof(decimal));
orders.columns.add("total",typeof(decimal),"subtotal*(1+taxrate)");
//-----link the table within a dataset.
dataset business = new dataset();
business.tables.add(customer);
business.tables.add(orders);
business.relations.add(customer.columns["cid"],orders.columns["customer"]);
customer.columns.add("ordertotals" ,typeof(decimal),"sum(child.total)");
return business;
}
private void btnaddcustomer_click(object sender, routedeventargs e)
{
//vist datatable customer.
datatable customer=business.tables["customer"];
newmember(customer);
lstdisplaycustomer.datacontext = customer;
}
private datatable newmember(datatable table)
{
datarow newrow = table.newrow();
newrow["cid"] = tbxcustomerid.text;
newrow["name"] = tbxcustomername.text;
table.rows.add(newrow);
return table;
}
private datatable newmemberorder(datatable table)
{
datarow newrow = table.newrow();
newrow["oid"] = tbxorderid.text;
newrow["customer"] = tbxordername.text;
newrow["subtotal"] = tbxsubtotal.text;
newrow["taxrate"] = tbxtaxrate.text;
table.rows.add(newrow);
return table;
}
private void btnaddorder_click(object sender, routedeventargs e)
{
//vist datatable order.
datatable order = business.tables["order"];
newmemberorder(order);
lstdisplayorder.datacontext = order;
}