Note that there are some explanatory texts on larger screens.

plurals
  1. PORelayCommand.CanExecute not updating IsEnabled in UI
    primarykey
    data
    text
    <p>I have a Windows Phone 8 app and I have a <code>RelayCommand</code> Instance called <code>DiscoverExpansionModulesCommand</code>. I have a button with the <code>Command</code> property bound to <code>DiscoverExpansionModulesCommand</code>. When the app first loads, the button is enabled or disabled properly. However, when on the page and I want to change whether the command can execute, the method <code>CanExecuteDiscoverExpansionModulesCommand()</code> properly fires and it returns the proper true or false value, but the button does not reflect it. Why isn't button updating it's UI? I found another article on this issue here <a href="http://social.msdn.microsoft.com/Forums/en-US/silverlightarchieve/thread/48a341e4-f512-4c33-befd-b614404b4920/" rel="nofollow">http://social.msdn.microsoft.com/Forums/en-US/silverlightarchieve/thread/48a341e4-f512-4c33-befd-b614404b4920/</a></p> <p>My ViewModel:</p> <pre><code>using GalaSoft.MvvmLight; using GalaSoft.MvvmLight.Command; using GalaSoft.MvvmLight.Messaging; using MAL.Portable.Commands; using MAL.Portable.Message; using MAL.Portable.Model; using System; using System.Collections.Generic; using System.Windows.Input; namespace MAL.Portable.ViewModel { public class SettingsViewModel : ViewModelBase { // Define an observable collection property that controls can bind to. private List&lt;Setting&gt; settings; private String controllerUrl; private String controllerPort; private String temperature; private Wifi wifi; private Boolean connected; private Boolean checkingConnection; public SettingsViewModel() { DiscoverExpansionModulesCommand = new RelayCommand(OnDiscoverExpansionModules, CanExecuteDiscoverExpansionModulesCommand); Messenger.Default.Register&lt;RetrieveSettingsMessage&gt; ( this, (action) =&gt; RetrievedListsMessage(action) ); Messenger.Default.Send&lt;GetSettingsMessage&gt;(new GetSettingsMessage()); } public ICommand DiscoverExpansionModulesCommand { get; private set; } public String ConnectionStatus { get { if (checkingConnection) return "checking"; else return connected ? "connected" : "not connnected"; } } private Boolean CanExecuteDiscoverExpansionModulesCommand() { return connected; } private void OnDiscoverExpansionModules() { } private void CheckConnection() { wifi = null; if (!String.IsNullOrWhiteSpace(ControllerUrl) &amp;&amp; !String.IsNullOrWhiteSpace(ControllerPort) &amp;&amp; !checkingConnection) { checkingConnection = true; wifi = new ReefAngelWifi(controllerUrl, controllerPort); wifi.TestConnectionComplete += wifi_TestConnectionComplete; wifi.RequestFail += wifi_RequestFail; wifi.BeginTestConnection(); } } private void wifi_RequestFail(object sender, RequestExceptionEventArgs e) { connected = false; checkingConnection = false; RaisePropertyChanged("ConnectionStatus"); } private void wifi_TestConnectionComplete(object sender, TestConnectionEventArgs e) { connected = e.TestSuccessful; checkingConnection = false; DiscoverExpansionModulesCommand.CanExecute(null); RaisePropertyChanged("ConnectionStatus"); RaisePropertyChanged("DiscoverExpansionModulesCommand"); } private object RetrievedListsMessage(RetrieveSettingsMessage action) { settings = action.Settings; CheckConnection(); return null; } private String GetStringValue(String key) { if (settings == null) return String.Empty; var item = settings.Find(x =&gt; x.Key == key); if (item == null) return String.Empty; else return item.Value; } private Boolean GetBooleanValue(String key) { if (settings == null) return false; var item = settings.Find(x =&gt; x.Key == key); if (item == null) return false; else return Boolean.Parse(item.Value); } } } </code></pre> <p>And the XAML</p> <pre><code>&lt;phone:PhoneApplicationPage xmlns:ReefAngel="clr-namespace:MAL.WindowsPhone8" xmlns:Controls="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.Input" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone" xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Platform.WP8" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:telerikPrimitives="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.Primitives" x:Class="MAL.WindowsPhone8.ReefAngel.SettingsPage" xmlns:converter="clr-namespace:MAL.WindowsPhone8.Converters" FontFamily="{StaticResource PhoneFontFamilyNormal}" FontSize="{StaticResource PhoneFontSizeNormal}" DataContext="{Binding Settings, Source={StaticResource Locator}}" SupportedOrientations="Portrait" Orientation="Portrait" mc:Ignorable="d" shell:SystemTray.IsVisible="True"&gt; &lt;phone:PhoneApplicationPage.Resources&gt; &lt;converter:BooleanToStringConverter x:Key="temperatureConverter" TrueString="Celsius" FalseString="Fahrenheit" /&gt; &lt;converter:BooleanToStringConverter x:Key="timeFormatConverter" TrueString="24 hour" FalseString="12 hour" /&gt; &lt;converter:BooleanToStringConverter x:Key="dateFormatConverter" TrueString="dd/mm/yyyy" FalseString="mm/dd/yyyy" /&gt; &lt;/phone:PhoneApplicationPage.Resources&gt; &lt;!--LayoutRoot is the root grid where all page content is placed--&gt; &lt;Grid x:Name="LayoutRoot" &gt; &lt;phone:Pivot Title="{Binding LocalizedResources.ApplicationTitle, Source={StaticResource LocalizedStrings}, StringFormat='\{0\} Settings'}"&gt; &lt;phone:PivotItem Header="connection"&gt; &lt;Grid&gt; &lt;StackPanel Margin="12,0,0,0"&gt; &lt;TextBlock Margin="0,20,0,0" TextWrapping="Wrap" Text="Reef Angel Wifi Address"/&gt; &lt;TextBox Height="72" TextWrapping="Wrap" Text="{Binding ControllerUrl, Mode=TwoWay}"/&gt; &lt;TextBlock Margin="0,20,0,0" TextWrapping="Wrap" Text="Reef Angel Wifi Port"/&gt; &lt;TextBox Height="72" TextWrapping="Wrap" Text="{Binding ControllerPort, Mode=TwoWay}"/&gt; &lt;StackPanel Orientation="Horizontal"&gt; &lt;TextBlock Margin="0,20,0,0" TextWrapping="Wrap" Text="Reef Angel Wifi Status : "/&gt; &lt;TextBlock Margin="0,20,0,0" TextWrapping="Wrap" Text="{Binding ConnectionStatus, Mode=OneWay}"/&gt; &lt;/StackPanel&gt; &lt;/StackPanel&gt; &lt;/Grid&gt; &lt;/phone:PivotItem&gt; &lt;phone:PivotItem Header="expansion"&gt; &lt;Grid&gt; &lt;Button Content="Discover Expansion Modules" x:Name="DiscoverButton" Command="{Binding DiscoverExpansionModulesCommand, Mode=OneWay}" /&gt; &lt;/Grid&gt; &lt;/phone:PivotItem&gt; &lt;/phone:Pivot&gt; &lt;/Grid&gt; &lt;/phone:PhoneApplicationPage&gt; </code></pre> <p>I am using the MVVM Light Portable Class Libraries.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload