由于不必烦恼某些人单击链接,因此该窗口的XAML代码共有约80行...
<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>
基本上,问题在于,甚至从窗口打开时起,该死的按钮(如下所示)就被禁用了。
这似乎正确绑定到the ViewModel。
在视图模型中,只有与按钮真正相关的东西才是命令及其执行的方法,如下所示:
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();
}
我不知道为什么按钮会被禁用或要提供什么其他信息,所以如果您需要更多信息,请发表评论。
为什么在禁用按钮的情况下打开窗口?