Sunday 15 May 2011

c# - Binding MenuItem's IsChecked to TabItem's isSelected -



c# - Binding MenuItem's IsChecked to TabItem's isSelected -

basically facing same problem describled @ binding menuitem's ischecked tabitem's isselected dynamic tabs customed tabcontrol have own viewmodel, had menu binds same source. happened binding menuitem's ischecked isselected did not work more. i thought isselected can not found there's no such property in viewmodel

<setter property="ischecked" value="{binding isselected, mode=twoway}" />

i tried utilize solution suggested build list of tabitem error unable cast object of type tabdata type tabitem. below xaml , converter. thought fails because during construction tabcontrol.items homecoming viewmodel instance instead of uicontrol tabitem; suggestions how binding here?

xaml

<menu background="transparent"> <menuitem style="{staticresource tabmenubuttonstyle}" itemssource="{binding relativesource= {relativesource findancestor, ancestortype={x:type tabcontrol}}, path=items,mode=oneway,notifyonsourceupdated=true,converter={staticresource tabcontrolitemconverter}}" itemcontainerstyle="{staticresource tabmenuitemxxx}"> </menuitem> </menu>

c#

public object convert(object value, type targettype, object parameter, system.globalization.cultureinfo culture) { itemcollection ic = (itemcollection)value; list<tabitem> tabitems = new list<tabitem>(); foreach (var obj in ic) { tabitems.add((tabitem)obj); } homecoming tabitems; }

here changes bases on provided project

remove next binding, not required

,mode=oneway,notifyonsourceupdated=true,converter={staticresource tabcontrolitemconverter}

modify setter in style tabmenuitemxxx

from

<setter property="ischecked" value="{binding path=isselected, mode=twoway, relativesource={relativesource ancestortype=tabitem}}" />

to

<setter property="ischecked" value="{binding path=isselected, mode=twoway/>

add next setter in style targettype="{x:type tabitem}"

<setter property="isselected" value="{binding isselected}" />

modify tabdata class follows

public class tabdata : inotifypropertychanged { private bool isselected; public string header { get; set; } public object content { get; set; } public bool isenabled { get; set; } public bool isselected { { homecoming isselected; } set { if (viewmodel.currentitem.isselected && viewmodel.currentitem != this) { viewmodel.currentitem.isselected = false; } isselected = value; raisepropertychanged("isselected"); if (viewmodel.currentitem != this) viewmodel.currentitem = this; } } public event propertychangedeventhandler propertychanged; public void raisepropertychanged(string propertyname) { propertychangedeventhandler handler = propertychanged; if (handler != null) handler(this, new propertychangedeventargs(propertyname)); } }

this need alter in project sync menu items's checked tab item's selected.

for sec issue closing tab item, can prepare changing close button's

commandparameter="{binding selecteditem,elementname=tabcontrol}"

to

commandparameter="{binding}"

sample project tabcontrolsyncwithmenuitems.zip

let me know results.

c# wpf xaml menuitem tabitem

No comments:

Post a Comment