c# - Create controltemplate in code, how to specify the converter in the binding -


i checked these answers, none had information i'm looking for:
how setup wpf datatemplate in code treeview?
how set control template in code?
create controltemplate programmatically in wpf

here gist of code:

datagridtextcolumn col = new datagridtextcolumn(); style styl = null; //        need add on per-column basis // <common:requiredpropertydisplaybrushconverter x:key="requireddisplaybrushconverter" />   string xaml = "<controltemplate targettype=\"{x:type datagridcell}\"> <textbox background=\"{binding relativesource={relativesource templatedparent}, path=content.text, converter={staticresource requireddisplaybrushconverter}  > </textbox> </controltemplate>"; memorystream sr = new memorystream(encoding.ascii.getbytes(xaml)); parsercontext pc = new parsercontext(); pc.xmlnsdictionary.add("", "http://schemas.microsoft.com/winfx/2006/xaml/presentation"); pc.xmlnsdictionary.add("x", "http://schemas.microsoft.com/winfx/2006/xaml"); controltemplate  ct = (controltemplate)xamlreader.load(sr, pc);  styl.setters.add(new setter(templateproperty, ct)); col.cellstyle = styl;   

in controltemplate, binding refers converter mentioned in comments. when converter defined in xaml resource datagrid, runtime error: {"cannot find resource named 'requireddisplaybrushconverter'. resource names case sensitive."}
can add resource column? or add datagrid's resources @ runtime?
or there other technique?
--

you can add xaml you'll need rearrange it. resource need defined before can use it. means background need defined child tag.

change xaml this:

string xaml = "<controltemplate targettype=\"{x:type datagridcell}\"><textbox><textbox.resources><common:requiredpropertydisplaybrushconverter x:key=\"requireddisplaybrushconverter\" /></textbox.resources><textbox.background><binding relativesource=\"{relativesource templatedparent}\" path=\"content.text\" converter=\"{staticresource requireddisplaybrushconverter}\"/></textbox.background></textbox></controltemplate>"; 

and you'll need define common namespace. add after other namespaces (with correct namespace/assembly, of course):

pc.xmlnsdictionary.add("common", "clr-namespace:wpfapplication;assembly=wpfapplication"); 

or, add resource app.xaml, if option.


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -