c# - TextBlock GetBindingExpression gets Null when text binded from resource file -
i have text block in text binded resource file,all works fine getbindingexpression return null. there other way of binding
<textblock x:name="slide" text="{x:static prop:resources.slidetocollect}"/> slide.getbindingexpression(textblock.textproperty)
x:static - not binding, , not creates bindingexpression instances. x:static - it wpf markup extension, allows reference static by-value code entity defined in cls–compliant way. in case - value resource.slidetocollect static property(or field, or constant, etc.) of resource class , assign text property of textblock.
if want use binding, need binding markup extension. here example code:
<window x:class="wpfapplication66.mainwindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:app="clr-namespace:wpfapplication66" title="mainwindow" height="350" width="525" > <window.resources> <app:resources x:key="resources"/> </window.resources> <grid> <textblock x:name="slide" text="{binding slidetocollect, source={staticresource resources}}"/> </grid>
and code behind window:
public class resources { public string slidetocollect { { return "i'am slidetocollect"; } } } public partial class mainwindow : window { public mainwindow() { initializecomponent(); bindingexpression = slide.getbindingexpression(textblock.textproperty); } }
now, variable "be" assigned instance of bindingexpression class. don't forget replace wpfapplication66 project name ;)