WPF Custom Control - Binding a template item to a Path -


in wpf custom control template, there way can following in xaml?:

var selitemtext = this.gettemplatechild("part_selecteditemtext") textblock; var binding = new binding("selecteditem." + displaymemberpath); binding.relativesource = new relativesource(relativesourcemode.templatedparent); selitemtext .setbinding(textblock.textproperty, binding); 

note interesting part of statement binding constructor - building path based on both text specify ("selecteditem."), , path provided user.

the consumer use control similar to:

<c:mycontrol displaymemberpath="description" /> 

short answer: no, it's not possible entirely in xaml within controltemplate

your possibilities are:

  • use have (possibly using attached properties / behavior make more mvvm-like)
  • use multibinding 1 binding "selecteditem" other "displaymemberpath" , multivalueconverter using reflection reflect down displaymemberpath (may bit ugly)
  • create class inherits binding , exposes properties can bind displaymemberpath , changes underlying binding (read here how can this) (complicated)
  • use reflection instantiate ms.internal.data.displaymembertemplateselector / build similar
  • think if design right. other control being kind of itemscontrol (if case should inherit itemscontrol , use displaymemberpath there), don't see why shouldn't use binding on outside <c:mycontrol displaymember="{binding selecteditem.description}" /> , in controltemplate use templatebinding bind "displaymember"

Comments

Popular posts from this blog

Javascript line number mapping -

c# - Is it possible to remove an existing registration from Autofac container builder? -

php - Mysql PK and FK char(36) vs int(10) -