wpf - Bind a button to the selected item -


i have button , want change click handler every time change tabs. hoping perform binding.

<window x:class="bwcrenameutility.mainwindow"         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"         xmlns:vpan="clr-namespace:bwcrenameutility.view.versionpanels"         title="mainwindow" height="526" width="525">     <grid>         <dockpanel>             <textblock text="foo" dockpanel.dock="top" textwrapping="wrap" padding="10" />             <grid dockpanel.dock="bottom">                 <!-- not correct, how perform binding correct? -->                 <button content="export..." horizontalalignment="right" margin="10" click="{binding selecteditem.content.performexport,elementname=tabcontrol}" />              </grid>             <tabcontrol name="tabcontrol">                 <tabitem header="1.2.5">                     <vpan:versionpanel1_2_5/>                 </tabitem>                 <tabitem header="1.2.8">                     <vpan:versionpanel1_2_8/> <!-- these can of same type inheritance -->                 </tabitem>             </tabcontrol>         </dockpanel>     </grid> </window> 

as can see, button.click not bound correctly , want know how works in wpf.

you can achieve commands, create icommand each of toy tabitem viewmodels , bind buttons command property command

the relaycommand common way handle stuff , can used throughout application

relay command:

   public class relaycommand : icommand    {        #region fields          readonly action<object> _execute;         readonly predicate<object> _canexecute;          #endregion          #region constructors          /// <summary>        /// initializes new instance of <see cref="relaycommand"/> class.        /// </summary>        /// <param name="execute">the execute.</param>        public relaycommand(action<object> execute) : this(execute, null) { }         /// <summary>        /// initializes new instance of <see cref="relaycommand"/> class.        /// </summary>        /// <param name="execute">the action execute.</param>        /// <param name="canexecute">the can execute.</param>        /// <exception cref="system.argumentnullexception">execute</exception>        public relaycommand(action<object> execute, predicate<object> canexecute)         {             if (execute == null)                throw new argumentnullexception("execute");             _execute = execute;            _canexecute = canexecute;        }          #endregion          #region icommand members          /// <summary>        /// defines method determines whether command can execute in current state.        /// </summary>        /// <param name="parameter">data used command.  if command not require data passed, object can set null.</param>        /// <returns>        /// true if command can executed; otherwise, false.        /// </returns>        [debuggerstepthrough]        public bool canexecute(object parameter)        {             return _canexecute == null ? true : _canexecute(parameter);        }         /// <summary>        /// occurs when changes occur affect whether or not command should execute.        /// </summary>        public event eventhandler canexecutechanged         {             add { commandmanager.requerysuggested += value; }             remove { commandmanager.requerysuggested -= value; }         }         /// <summary>        /// defines method called when command invoked.        /// </summary>        /// <param name="parameter">data used command.  if command not require data passed, object can set null.</param>        public void execute(object parameter)        {            _execute(parameter);         }          #endregion     } 


and use in following fashion in application

viewmodel or control:

public class versionpanel1_2_8 : versionpanel {     public icommand mycommand { get; internal set; }      public versionpanel1_2_8()     {           mycommand = new relaycommand(x => methodtoexecute());     }      private void methodtoexecute()     {      } } 

xaml:

<window x:class="bwcrenameutility.mainwindow"         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"         xmlns:vpan="clr-namespace:bwcrenameutility.view.versionpanels"         title="mainwindow" height="526" width="525">     <grid>         <dockpanel>             <textblock text="foo" dockpanel.dock="top" textwrapping="wrap" padding="10" />             <grid dockpanel.dock="bottom">                 <!-- not correct, how perform binding correct? -->                 <button content="export..." horizontalalignment="right" margin="10" command="{binding selecteditem.content.mycommand,elementname=tabcontrol}" />              </grid>             <tabcontrol name="tabcontrol">                 <tabitem header="1.2.5">                     <vpan:versionpanel1_2_5/>                 </tabitem>                 <tabitem header="1.2.8">                     <vpan:versionpanel1_2_8/> <!-- these can of same type inheritance -->                 </tabitem>             </tabcontrol>         </dockpanel>     </grid> </window> 

Comments

Popular posts from this blog

javascript - DIV "hiding" when changing dropdown value -

Does Firefox offer AppleScript support to get URL of windows? -

android - How to install packaged app on Firefox for mobile? -