0

Since some people can't be bothered to click on a link, here's all ~80 lines of XAML code for the window...

<Window x:Class="TollLicenser.Views.LicenseView"
        x:Name="wdw"
        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:TollLicenser.Views"
        mc:Ignorable="d"
        Title="License View" Height="300" Width="300"
        xmlns:vm="clr-namespace:TollLicenser.ViewModels"
        d:DataContext="{d:DesignInstance vm:LicenseViewModel}"
        SizeToContent="WidthAndHeight"
        Margin="8">
    <Window.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Button.xaml" />
                <ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Shadows.xaml" />
                <ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.ToggleButton.xaml" />
            </ResourceDictionary.MergedDictionaries>
            <Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource MaterialDesignTextBox}">
                <Setter Property="Margin" Value="0 8 8 8" />
            </Style>
            <Style TargetType="{x:Type DatePicker}">
                <Setter Property="Margin" Value="0 8 8 8" />
            </Style>
            <Style TargetType="{x:Type Label}">
                <Setter Property="Margin" Value="0 8 8 8" />
            </Style>
            <Style TargetType="{x:Type TextBlock}">
                <Setter Property="Margin" Value="8" />
            </Style>
        </ResourceDictionary>
    </Window.Resources>
    <Grid FocusManager.FocusedElement="{Binding ElementName=CountryCode}">
        <Grid.RowDefinitions>
            <RowDefinition Height="auto"/>
            <RowDefinition Height="auto"/>
            <RowDefinition Height="auto"/>
            <RowDefinition Height="auto"/>
            <RowDefinition Height="auto"/>
            <RowDefinition Height="auto"/>
            <RowDefinition Height="5"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="auto"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>

        <TextBlock Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2"
               Style="{StaticResource MaterialDesignHeadlineTextBlock}">
            Managing a License
        </TextBlock>

        <Label Grid.Row="1" Grid.Column="0">Country Code</Label>
        <TextBox Grid.Row="1" Grid.Column="1" Name="CountryCode" Text="{Binding CountryCode}"/>

        <Label Grid.Row="2" Grid.Column="0">Plaza Name</Label>
        <TextBox Grid.Row="2" Grid.Column="1" Text="{Binding TollPlazaName}"/>

        <Label Grid.Row="3" Grid.Column="0">Start Date</Label>
        <DatePicker Grid.Row="3" Grid.Column="1" SelectedDate="{Binding StartDate}"/>

        <Label Grid.Row="4" Grid.Column="0">End Date</Label>
        <DatePicker Grid.Row="4" Grid.Column="1" SelectedDate="{Binding EndDate}"/>

        <Label Grid.Row="5" Grid.Column="0">Max Users</Label>
        <TextBox Grid.Row="5" Grid.Column="1" Text="{Binding MaxUsers}"/>

        <GridSplitter Grid.Row="6" Height="5" />

        <Button Grid.Row="7" Grid.Column="0" Content="Save" Margin="8" IsEnabled="True" IsDefault="True"
                Command="{Binding SaveLicenseCommand}" CommandParameter="{Binding ElementName=wdw}" />
    </Grid>
</Window>

Basically the problem is that the damn button (seen below) is disabled even from the time the window opens...

This seems to bind correctly to the ViewModel.

Only things in the viewmodel that have anything really to do with the button is the command and the method it executes as below:

public ICommand SaveLicenseCommand => new AwaitableDelegateCommand<Window>(SaveLicense);
private async Task SaveLicense(Window wdw)
{
    _model.Id = Id;
    _model.CountryCode = Encryption.Encrypt(CountryCode);
    _model.EndDate = Encryption.Encrypt(EndDate.ToString());
    _model.MaxUsers = Encryption.Encrypt(MaxUsers.ToString());
    _model.StartDate = Encryption.Encrypt(StartDate.ToString());
    _model.TollPlazaName = Encryption.Encrypt(TollPlazaName);

    // LYTODO Post the model to the web service.
    if (_model.Id != 0)
    {
        // Editing a license.
        await _apiHelper.PutLicenseAsync(_model);
        await _licenses.Load(); // Reload licenses with the updated item.
    }
    else
    {
        // Adding a license.
        await _apiHelper.PostLicenseAsync(_model);
    }

    wdw.Close();
}

I don't have the faintest idea why the button is disabled or what other information to provide so please comment if there's more that you need and I will include it.

Why is the window opening with the button disabled?

Ortund
  • 8,095
  • 18
  • 71
  • 139
  • 1
    give your xaml code – RackM Jul 11 '18 at 14:14
  • @RackM the whole window's markup is linked on the first line of the question. – Ortund Jul 11 '18 at 14:15
  • 1
    That's usually a bug in the `ICommand` implmemenation. Try calling `CommandManager.InvalidateRequerySuggested()` . https://stackoverflow.com/questions/1340302/wpf-how-to-force-a-command-to-re-evaluate-canexecute-via-its-commandbindings –  Jul 11 '18 at 14:15
  • 1
    @MickyD incidentally the `CommandManager.InvalidateRequerySuggested()` issue appears to actually have been the bug. I copied my code from a different project which correctly implements the same model and now the button isn't disabled. Don't know what mistake I made this time though... – Ortund Jul 11 '18 at 14:25
  • Don't worry. I've had it happen to me a few times too. Can never work out the exact reason even though the same class is being used elsewhere fine –  Jul 11 '18 at 14:26
  • How is your `AwaitableDelegateCommand` class implemented? – mm8 Jul 11 '18 at 14:47

0 Answers0