OK, the XAML is quite simple and uses MVVM to bind to an ICommand SomeCommand { get; } property on a view model:
<Button Command="{Binding Path=SomeCommand}">Something</Button>
If SomeCommand returns null, the button is enabled. (Nothing to do with CanExecute(object param) method on ICommand, because there is no instance to call that method on)
And now the question: Why is the button enabled? How would you work it around?
If you press the "enabled" button, obviously nothing is called. It is just ugly that the button looks enabled.
My colleague found an elegant solution: using a binding fallback value!
public class NullCommand : ICommand
{
private static readonly Lazy<NullCommand> _instance = new Lazy<NullCommand>(() => new NullCommand());
private NullCommand()
{
}
public event EventHandler CanExecuteChanged;
public static ICommand Instance
{
get { return _instance.Value; }
}
public void Execute(object parameter)
{
throw new InvalidOperationException("NullCommand cannot be executed");
}
public bool CanExecute(object parameter)
{
return false;
}
}
And then the XAML looks like:
<Button Command="{Binding Path=SomeCommand, FallbackValue={x:Static local:NullCommand.Instance}}">Something</Button>
The advantage of this solution is that it works better if you break Law of Demeter and you have some dots in the binding path, where each instance might become null.
It is enabled because that's the default state. Disabling it automatically would be an arbitrary measure that gives rise to other problems.
If you want to have a button without an associated command be disabled, bind the IsEnabled property to SomeCommand using an appropriate converter, e.g.:
[ValueConversion(typeof(object), typeof(bool))]
public class NullToBooleanConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return value !== null;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotSupportedException();
}
}
Very similar to Jon's answer, you could use a style with a trigger to mark buttons which should be disabled when there is no command set.
<Style x:Key="CommandButtonStyle"
TargetType="Button">
<Style.Triggers>
<Trigger Property="Command"
Value="{x:Null}">
<Setter Property="IsEnabled"
Value="False" />
</Trigger>
</Style.Triggers>
</Style>
I prefer this solution because it addresses the problem very directly and it does not require any new types.
Related
I want to compare two dynamic values User_id and user_id for equality and setting one property Cursor. Also, when the cursor is hand, I have to execute one function. How to do it? This is the code that I am using:
<DataTrigger Binding="{Binding Path=User_id}" Value="{Binding Path=user_id}">
<Setter Property="Cursor" Value="Hand"/>
</DataTrigger>
There are a couple options to attack this.
#1. Multibinding Converter
You can use Multibindingto input the two values into a IMultiValueConverter. To use this type of binding in your DataTrigger, you would use follow the following syntax.
<DataTrigger Value="True">
<DataTrigger.Binding>
<MultiBinding>
<MultiBinding.Converter>
<local:EqualityConverter />
</MultiBinding.Converter>
<Binding Path="User_id" />
<Binding Path="user_id" />
</MultiBinding>
</DataTrigger.Binding>
<Setter Property="Window.Cursor" Value="Hand"/>
</DataTrigger>
The MultiBinding.Converteris set to a new instance of EqualityConverter, which is a class I created that implements the IMultiValueConverter interface. This class will do the comparison for you. The DataTrigger triggers when this converter returns true.
public class EqualityConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
if (values.Length < 2)
return false;
return values[0].Equals(values[1]);
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
#2. MVVM Pattern
I'm not sure where your DataContext is coming from, but if possible, you may want to consider using a view model for your binding. The view model could expose a property that does the equality comparison for you. Something like this.
public class UserViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private int _User_id;
private int _user_id;
public int User_id
{
get
{
return _User_id;
}
set
{
if (_User_id != value)
{
_User_id = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("User_id"));
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("IsUserIdsEqual"));
DoSomething();
}
}
}
public int user_id
{
get
{
return _user_id;
}
set
{
if (_user_id != value)
{
_user_id = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("user_id"));
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("IsUserIdsEqual"));
DoSomething();
}
}
}
public bool IsUserIdsEqual
{
get { return _user_id == _User_id; }
}
private void DoSomething()
{
if (this.IsUserIdsEqual)
{
//Do something when they are equal.
}
}
}
If using a view model like this, your DataTrigger could simplify to..
<DataTrigger Binding="{Binding Path=IsUserIdsEqual}" Value="True">
<Setter Property="Window.Cursor" Value="Hand"/>
</DataTrigger>
Regarding executing a function on the trigger, I added a DoSomething method to highlight how the view model could be used to execute a function when the two IDs are equal. I'm not sure if that would work for your case because I'm not sure what the intent of the function call is, but it is a way to execute a function when a condition changes.
Consider pretty much standard Attached Property for some derived type:
public class DerivedKeyBinding : KeyBinding
{
public string Name; //shortened, make this propdp
}
public class KeyBindingExtenstions
{
public static DerivedKeyBinding GetCommand(DependencyObject obj) {
return (DerivedKeyBinding)obj.GetValue(CommandProperty);
}
public static void SetCommand(DependencyObject obj, DerivedKeyBinding value) {
obj.SetValue(CommandProperty, value);
}
public static readonly DependencyProperty CommandProperty =
DependencyProperty.RegisterAttached("Command", typeof(DerivedKeyBinding), typeof(KeyBindingExtenstions), new UIPropertyMetadata(null, CommandChanged));
private static void CommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) {
var commandValue = GetCommand(d);
}
}
which is later set in XAML:
<Grid>
<Grid.Style>
<Style>
<Setter Property="local:KeyBindingExtenstions.Command">
<Setter.Value>
<local:DerivedKeyBinding Name="{Binding Title, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}}"/>
</Setter.Value>
</Setter>
</Style>
</Grid.Style>
</Grid>
This will, however, crash on the var commandValue = GetCommand(d); line due to local:DerivedKeyBinding lost to KeyBinding somewhere in process.
For some weird reason the property of DerivedKeyBinding type is being set with value of type KeyBinding (even though value is explicitly set to DerivedKeyBinding in XAML).
System.InvalidCastException: 'Unable to cast object of type 'System.Windows.Input.KeyBinding' to type 'AttachedPropertyInStyle.DerivedKeyBinding'.'
Why is this happening and how can I fix the issue?
The issue seems to be connected to the Name binding - if it is static value, the code executes flawlessly.
Override KeyBinding.CreateInstanceCore. From Microsoft Docs:
Notes to Inheritors
Every Freezable derived class must implement this method. A typical implementation is to simply call the default constructor and return the result.
public class DerivedKeyBinding : KeyBinding
{
...
protected override Freezable CreateInstanceCore()
{
return new DerivedKeyBinding();
}
}
In the PropertyChangedCallback you may also write:
var commandValue = (DerivedKeyBinding)e.NewValue;
I have got a view model with a property:
public class MyModel
{
public bool IsEnabled {get;set;}
}
I want to use this property to toggle a button state. If the boolean is true I want to hide the button, and otherwise show it.
I tried things like:
<Button Visibility= "{Binding IsEnabled ? Hidden : Visible }">Enable</Button>
But this doesn't fit.
I tried some more complex solution but my guess is, I am missing something trivial.
Any suggestions?
Since you want to toggle between Hidden and Visible and true is hidden you can either write custom IValueConverter or use simple Style.Trigger
<Button Content="Enable">
<Button.Style>
<Style TargetType="{x:Type Button}">
<Setter Property="Visibility" Value="Visible"/>
<Style.Triggers>
<DataTrigger Binding="{Binding IsEnabled}" Value="True">
<Setter Property="Visibility" Value="Hidden"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
This is all assuming the DataContext is set accordingly and MyModel.IsEnabled raises INotifyPropertyChanged.PropertyChanged event whenever changed
public class MyModel : INotifyPropertyChanged
{
private bool _isEnabled;
public bool IsEnabled
{
get { return _isEnabled; }
set
{
_isEnabled = value;
OnPropertyChanged("IsEnabled");
}
}
#region INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
var handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
#endregion
}
Use the BooleanToVisibilityConverter:
<Window.Resources>
<BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
</Window.Resources>
<Button Visibility= "{Binding IsEnabled, Converter={StaticResource BooleanToVisibilityConverter}}" />
Add a class inheriting IValueConverter
public class BooleanToVisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
bool bValue = (bool)value;
if (bValue)
return Visibility.Visible;
else
return Visibility.Collapsed;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
Visibility visibility = (Visibility)value;
if (visibility == Visibility.Visible)
return true;
else
return false;
}
#endregion
}
I have a view that I would like to assign a "backup" viewmodel to. Essentially if "Generic" is null I would like to set the DataContext to "GenericFactory". "GenericFactory" is able to create an instance of the "Generic" viewmodel. Upon creation the viewmodel is assigned to the appropriate property and the PropertyChanged event is fired, however given the code below the only DataContext I'm ever bound to is "GenericFactory". Can anyone explain and/or offer an alternative solution?
XAML
<Page x:Class="GenericProject.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vw="clr-namespace:GenericProject.View">
<StackPanel>
<!--Additional markup-->
<vw:GenericView>
<vw:GenericView.Style>
<Style TargetType="{x:Type vw:GenericView}">
<Setter Property="DataContext" Value="{Binding Generic}" />
<Style.Triggers>
<DataTrigger Binding="{Binding Generic}" Value="{x:Null}">
<Setter Property="DataContext" Value="{Binding GenericFactory}" />
</DataTrigger>
</Style.Triggers>
</Style>
</vw:GenericView.Style>
</vw:GenericView>
</StackPanel>
</Page>
ViewModel
public class MainPageViewModel : ViewModelBase
{
public GenericViewModel Generic
{
get { return _generic; }
private set
{
if (_generic != value)
{
_generic = value;
base.OnPropertyChanged("Generic");
}
}
}
public GenericFactoryViewModel GenericFactory { get; private set; }
private void OnGenericFactoryCreatedGeneric(object sender, CreatedGenericEventArgs e)
{
Generic = e.Generic;
}
public MainPageViewModel()
{
GenericFactory = new GenericFactoryViewModel();
GenericFactory.CreatedGeneric += OnGenericFactoryCreatedGeneric;
}
}
Thanks - Derrick
Thanks to XAMIMAX's comment I was able to find a solution using PriorityBinding.
XAML
<Page x:Class="GenericProject.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:GenericProject"
xmlns:vw="clr-namespace:GenericProject.View">
<Page.Resources>
<local:NullToDependencyPropertyUnsetConverter x:Key="NullToDependencyPropertyUnsetConverter" />
</Page.Resources>
<StackPanel>
<!--Additional markup-->
<vw:GenericView>
<vw:GenericView.DataContext>
<PriorityBinding>
<Binding Path="Generic" Converter="{StaticResource NullToDependencyPropertyUnsetConverter}" />
<Binding Path="GenericFactory" />
</PriorityBinding>
</vw:GenericView.DataContext>
</vw:GenericView>
</StackPanel>
</Page>
Converter
public class NullToDependencyPropertyUnsetConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return value ?? DependencyProperty.UnsetValue;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
I don't know how your factory works so this is probably not working code but you should be handling this logic in the view model, your view should just set the datacontext.
public GenericViewModel Generic
{
get
{
if(_generic == null)
{
GenericFactory.Create();
}
return _generic;
}
private set
{
if (_generic != value)
{
_generic = value;
base.OnPropertyChanged("Generic");
}
}
}
This will return null for Generic but when OnGenericFactoryCreatedGeneric is called it will set Generic and then cause the binding to update to a newly created view model.
If your factory has a synchronous create that returns the ViewModel then that would be better as Generic will never return null. _generic = GenericFactory.Create();
I've got a collection of ViewModels that are rendered as tabs using a style to pull out the relevant content to display on the tab:
public class TabViewModel : DependencyObject
{
public object Content
{
get { return (object)GetValue(ContentProperty); }
set
{
SetValue(ContentProperty, value);
}
}
}
Here's the TabControl:
<TabControl
ItemsSource={Binding MyCollectionOfTabViewModels}"
ItemContainerStyle="{StaticResource TabItemStyle}" />
And here's the style
<Style TargetType="TabItem" x:Key="TabItemStyle">
<Setter Property="Content" Value="{Binding Content}"/>
</Style>
We are creating an instance of a usercontrol and setting the "Content" property of the TabViewModel to that so that the usercontrol gets displayed in the TabItem's Content area.
MyCollectionOfViewModels.Add(new TabViewModel()
{
Content = new MyUserControl();
});
My question is, I would like to allow a MyUserControl (or any of its sub controls) added to the TabViewModel's Content property to be allowed to raise an event that the TabViewModel handles.
Anyone know how I would do that?
We've experimented using RoutedEvents and RoutedCommands, but haven't been able to get anything to work 100% and have it be compatible with MVVM. I really think that this could be done with a RoutedEvent or RoutedCommand, but I don't seem to be able to get this to work.
Note: I've removed some of the relevant Prism-specific code, but if you are wondering why we do something so silly, it is because we are trying to stay control agnostic by using Prism's RegionManager.
You could add a State property to your TabViewModel, and check the DependencyPropertyChanged events.
So imagine the following enum:
public enum TabViewModelState
{
True,
False,
FileNotFound
}
Then add a State property to your TabViewModel of this enum:
public static readonly DependencyProperty StateProperty =
DependencyProperty.Register("State", typeof(TabViewModelState), typeof(TabViewModel), new PropertyMetadata(OnStateChanged));
private static void OnStateChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
TabViewModel viewModel= (TabViewModel)obj;
//Do stuff with your viewModel
}
Use a two-way binding to this property in your control:
<CheckBox Checked="{Binding Path=State, Converter={StaticResource StateToBooleanConverter}, Mode=TwoWay}" />
And last but not least implement the converter that will convert to and from the original value needed for the control.. (in my example boolean <--> TabViewModelState):
public class StateToBooleanConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
TabViewModelState state = (TabViewModelState) value;
return state == TabViewModelState.True;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
bool result = (bool) value;
return result ? TabViewModelState.True : TabViewModelState.False;
}
}
So now you have a State property that is managed by the UI, and throws changed events when you need to respond..
Hope this helps!
If you put a command on your ViewModel and just bind to that from your UserControl it will fire anyway. You don't have to bubble it, the UserControl will just find the command and use it.
If you had a delegate command 'GoCommand' on your ViewModel, your UserControls button binding would just look like this:
<Button Command="{Binding GoCommand}" >Go</Button>
I went through the same thought process thinking that the UserControl needs to bubble the command, but it dosn't - the binding will just hook itself up when it finds the command on the ViewModel.
Hope this helps and I havn't missed the point! ;)
You should have a look at Marlon Grech's Attached Command Behavior, which allows you to attach a ViewModel command to an GUI event.