c# - WPF: Command hindering button to enable -
i have 2 buttons.. @ beginning 1 of them enabled , other not. have 2 commands these buttons need implement keybindings them. these commands should replace click-methods buttons.
when first button clicked second button should become enabled. not working. if remove command of second button , add click-method instead works..
xaml:
<window x:class="wpfapplication1.mainwindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:wpfapplication1" mc:ignorable="d" title="mainwindow" height="350" width="525"> <window.resources> <routeduicommand x:key="first" /> <routeduicommand x:key="second" /> </window.resources> <window.commandbindings> <commandbinding command="{staticresource first}" executed="commandbinding_executed_1" /> <commandbinding command="{staticresource second}" canexecute="commandbinding_canexecute" executed="commandbinding_executed" /> </window.commandbindings> <window.inputbindings> <keybinding key="f4" command="{staticresource first}" /> <keybinding key="f5" command="{staticresource second}" /> </window.inputbindings> <grid> <button command="{staticresource first}" x:name="button" content="button" horizontalalignment="left" margin="154,146,0,0" verticalalignment="top" width="75"/> <button command="{staticresource second}" isenabled="false" x:name="button1" content="button" horizontalalignment="left" margin="312,146,0,0" verticalalignment="top" width="75"/> </grid> </window>
xaml.cs
using system.windows; using system.windows.input; namespace wpfapplication1 { /// <summary> /// interaction logic mainwindow.xaml /// </summary> public partial class mainwindow : window { public mainwindow() { initializecomponent(); } private void commandbinding_executed(object sender, executedroutedeventargs e) { messagebox.show("test"); } private void commandbinding_canexecute(object sender, canexecuteroutedeventargs e) { e.canexecute = button1.isenabled; } private void commandbinding_executed_1(object sender, executedroutedeventargs e) { button1.isenabled = true; } } }
when canexecute
defined, button en- or disabled based on value of e.canexecute
, therefor setting isenabled
explicitly has no effect, since set implicitly.
you introduce variable returned commandbinding_canexecute
- have desired effect far can tell.
public partial class mainwindow : window { // other details elided bool _enablebutton1 = false; private void commandbinding_canexecute(object sender, canexecuteroutedeventargs e) { e.canexecute = _enablebutton1; } private void commandbinding_executed_1(object sender, executedroutedeventargs e) { enablebutton1 = true; } }
anyway, elide canexecute
-action altogether , set isenabled
explicitly. trick well.
public partial class mainwindow : window { // elided private void commandbinding_executed_1(object sender, executedroutedeventargs e) { button1.isenabled = true; } }
Comments
Post a Comment