Datagrid actual width
on aspnet page, have following plugin:
<div id="div_container" >
<asp:scriptmanager runat="server"></asp:scriptmanager>
<asp:silverlight source="mysilverlight.xap" runat="server"></asp:silverlight>
</div>
page.xaml.vb code:
private sub loadgrid()
dim ub as new uribuilder("myurl")
dim wc as new webclient()
addhandler wc.downloadstringcompleted, addressof mydownloadstringcompleted
wc.downloadstringasync(ub.uri)
end sub
private sub mydownloadstringcompleted()
'code populate datagrid .. . .
end sub
private sub mygrid_sizechanged(byval sender as object, byval e as system.windows.sizechangedeventargs) handles mygrid.sizechanged
mygrid.dispatcher.begininvoke(addressof updateui)
end sub
private sub updateui()
myxamltext.text = mygrid.actualwidth
'uncommenting 1 of 2 lines below, return incorrect actualwidth value
'htmlpage.window.invoke("setpluginwidth", mygrid.actualwidth)
'htmlpage.document.getelementbyid("div_container").setstyleattribute("width", mygrid.actualwidth)
end sub
what i'm trying do, resizing plugin container div element same size datagrid. this, added silverlight textbox (myxamltext) debugging, shows datagrid actualwidth after populated. if run code above, myxamltext shows actualwidth correclty, , if uncomment code call javascript function set div width, or uncomment line ".. .setstyleattribute.. .", then myxamltext show incorrect actual width value. when debugging, noticed without these 2 lines, sub "updateui" hit more once increment in calculated actualwidth, until reach correct value, while when including 2 lines, "updateui" sub hit once. sure i'm missing here don't understand. i'm new silverlight , first try, please bare me
thanks
ok, got working following post jeff wilcox.
here xaml:
<usercontrol x:class="silverlightplay.page" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:data="clr-namespace:system.windows.controls;assembly=system.windows.controls.data" > <grid x:name="layoutroot"> <data:datagrid x:name="dg"/> </grid> </usercontrol>
code behind:
using system; using system.collections.generic; using system.linq; using system.net; using system.windows; using system.windows.controls; using system.windows.documents; using system.windows.input; using system.windows.media; using system.windows.media.animation; using system.windows.shapes; using system.windows.browser; using system.windows.threading; namespace silverlightplay { public partial class page : usercontrol { public page() { initializecomponent(); dg.itemssource = new list() { new mydata() {id="0",name="john doe",somevalue="september",anothervalue="checking"}, new mydata() {id="1",name="jane doe",somevalue="october",anothervalue="savings"}, new mydata() {id="2",name="jack doe",somevalue="november",anothervalue="moneymarket"}, new mydata() {id="3",name="jim doe",somevalue="december",anothervalue="checking"}, new mydata() {id="4",name="jill doe",somevalue="january",anothervalue="savings"} }; } protected override size measureoverride(size availablesize) { var youcanbeasbigasyouwant = new size(double.positiveinfinity, double.positiveinfinity); dg.measure(youcanbeasbigasyouwant); var size = dg.desiredsize; htmlelement plugin = htmlpage.plugin; htmlelement host = plugin.parent; plugin.setstyleattribute("width", size.width.tostring()); plugin.setstyleattribute("height", size.height.tostring()); host.setstyleattribute("width", size.width.tostring()); host.setstyleattribute("height", size.height.tostring()); return base.measureoverride(availablesize); } public class mydata { public string id { get; set; } public string name { get; set; } public string somevalue { get; set; } public string anothervalue { get; set; } } } }
and html page:
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <!-- saved url=(0014)about:internet --> <head> <title>silverlightplay</title> <style type="text/css"> html, body { height: 100%; overflow: auto; } body { padding: 0; margin: 0; } </style> </head> <body> <div> <h1>customer information grid</h1> </div> <div id="silverlightcontrolhost" style="background-color: green;width:176px;height:100px;"> <object id="slhost" data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="100%" height="100%"> <param name="source" value="clientbin/silverlightplay.xap"/> <param name="background" value="white" /> <param name="minruntimeversion" value="2.0.31005.0" /> <param name="autoupgrade" value="true" /> <a href="http://go.microsoft.com/fwlink/?linkid=124807" style="text-decoration: none;"> <img src="http://go.microsoft.com/fwlink/?linkid=108181" alt="get microsoft silverlight" style="border-style: none"/> </a> </object> <iframe style='visibility:hidden;height:0;width:0;border:0px'></iframe> </div> <div> <input type=button onclick="blocked scriptalert(document.getelementbyid('slhost').width);" value="get width" /> </div> </body> </html>
it sizes size of grid loads.
Silverlight > Programming Silverlight with JavaScript
Comments
Post a Comment