Browse Source

Buttons are disabled when there is a null in the binding chain for Command.

Fixes #846.
pull/894/head
Jeremy Koritzinsky 10 years ago
parent
commit
64e0905d61
  1. 1
      samples/BindingTest/BindingTest.csproj
  2. 3
      samples/BindingTest/MainWindow.xaml
  3. 8
      samples/BindingTest/ViewModels/MainWindowViewModel.cs
  4. 20
      samples/BindingTest/ViewModels/NestedCommandViewModel.cs
  5. 21
      src/Avalonia.Controls/Button.cs
  6. 2
      src/Avalonia.Controls/MenuItem.cs

1
samples/BindingTest/BindingTest.csproj

@ -85,6 +85,7 @@
<Compile Include="ViewModels\IndeiErrorViewModel.cs" />
<Compile Include="ViewModels\ExceptionErrorViewModel.cs" />
<Compile Include="ViewModels\MainWindowViewModel.cs" />
<Compile Include="ViewModels\NestedCommandViewModel.cs" />
<Compile Include="ViewModels\TestItem.cs" />
</ItemGroup>
<ItemGroup>

3
samples/BindingTest/MainWindow.xaml

@ -95,8 +95,9 @@
<Button Content="Button" Command="{Binding StringValueCommand}" CommandParameter="Button"/>
<ToggleButton Content="ToggleButton" IsChecked="{Binding BooleanFlag, Mode=OneWay}" Command="{Binding StringValueCommand}" CommandParameter="ToggleButton"/>
<CheckBox Content="CheckBox" IsChecked="{Binding !BooleanFlag, Mode=OneWay}" Command="{Binding StringValueCommand}" CommandParameter="CheckBox"/>
<RadioButton Content="RadionButton" IsChecked="{Binding !!BooleanFlag, Mode=OneWay}" Command="{Binding StringValueCommand}" CommandParameter="RadioButton"/>
<RadioButton Content="Radio Button" IsChecked="{Binding !!BooleanFlag, Mode=OneWay}" Command="{Binding StringValueCommand}" CommandParameter="RadioButton"/>
<TextBox Text="{Binding Path=StringValue}"/>
<Button Content="Nested View Model Button" Name="NestedTest" Command="{Binding NestedModel.Command}" />
</StackPanel>
</TabItem>
</TabControl>

8
samples/BindingTest/ViewModels/MainWindowViewModel.cs

@ -15,6 +15,7 @@ namespace BindingTest.ViewModels
private string _stringValue = "Simple Binding";
private bool _booleanFlag = false;
private string _currentTime;
private NestedCommandViewModel _nested;
public MainWindowViewModel()
{
@ -39,6 +40,7 @@ namespace BindingTest.ViewModels
{
BooleanFlag = !BooleanFlag;
StringValue = param.ToString();
NestedModel = _nested ?? new NestedCommandViewModel();
});
Task.Run(() =>
@ -90,5 +92,11 @@ namespace BindingTest.ViewModels
public DataAnnotationsErrorViewModel DataAnnotationsValidation { get; } = new DataAnnotationsErrorViewModel();
public ExceptionErrorViewModel ExceptionDataValidation { get; } = new ExceptionErrorViewModel();
public IndeiErrorViewModel IndeiDataValidation { get; } = new IndeiErrorViewModel();
public NestedCommandViewModel NestedModel
{
get { return _nested; }
private set { this.RaiseAndSetIfChanged(ref _nested, value); }
}
}
}

20
samples/BindingTest/ViewModels/NestedCommandViewModel.cs

@ -0,0 +1,20 @@
using ReactiveUI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
namespace BindingTest.ViewModels
{
public class NestedCommandViewModel : ReactiveObject
{
public NestedCommandViewModel()
{
Command = ReactiveCommand.Create();
}
public ICommand Command { get; }
}
}

21
src/Avalonia.Controls/Button.cs

@ -4,6 +4,7 @@
using System;
using System.Linq;
using System.Windows.Input;
using Avalonia.Data;
using Avalonia.Input;
using Avalonia.Interactivity;
using Avalonia.Rendering;
@ -41,8 +42,9 @@ namespace Avalonia.Controls
/// <summary>
/// Defines the <see cref="Command"/> property.
/// </summary>
public static readonly StyledProperty<ICommand> CommandProperty =
AvaloniaProperty.Register<Button, ICommand>(nameof(Command));
public static readonly DirectProperty<Button, ICommand> CommandProperty =
AvaloniaProperty.RegisterDirect<Button, ICommand>(nameof(Command),
button => button.Command, (button, command) => button.Command = command, enableDataValidation: true);
/// <summary>
/// Defines the <see cref="HotKey"/> property.
@ -68,6 +70,8 @@ namespace Avalonia.Controls
public static readonly RoutedEvent<RoutedEventArgs> ClickEvent =
RoutedEvent.Register<Button, RoutedEventArgs>("Click", RoutingStrategies.Bubble);
private ICommand _command;
/// <summary>
/// Initializes static members of the <see cref="Button"/> class.
/// </summary>
@ -102,8 +106,8 @@ namespace Avalonia.Controls
/// </summary>
public ICommand Command
{
get { return GetValue(CommandProperty); }
set { SetValue(CommandProperty, value); }
get { return _command; }
set { SetAndRaise(CommandProperty, ref _command, value); }
}
/// <summary>
@ -240,6 +244,15 @@ namespace Avalonia.Controls
}
}
protected override void UpdateDataValidation(AvaloniaProperty property, BindingNotification status)
{
base.UpdateDataValidation(property, status);
if(property == CommandProperty)
{
IsEnabled = status?.ErrorType != BindingErrorType.Error;
}
}
/// <summary>
/// Called when the <see cref="Command"/> property changes.
/// </summary>

2
src/Avalonia.Controls/MenuItem.cs

@ -24,7 +24,7 @@ namespace Avalonia.Controls
/// Defines the <see cref="Command"/> property.
/// </summary>
public static readonly StyledProperty<ICommand> CommandProperty =
Button.CommandProperty.AddOwner<MenuItem>();
AvaloniaProperty.Register<MenuItem, ICommand>(nameof(Command));
/// <summary>
/// Defines the <see cref="HotKey"/> property.

Loading…
Cancel
Save