I want to pass an enum value as command parameter in WPF, using something like this:
<Button
x:Name="uxSearchButton"
Command="{Binding Path=SearchMembersCommand}"
CommandParameter="SearchPageType.First"
Content="Search">
</Button>
SearchPageType is an enum and this is to know from which button search command is invoked.
Is this possible in WPF, or how can you pass an enum value as command parameter?
Try this
<Button CommandParameter="{x:Static local:SearchPageType.First}" .../>
local - is your namespace reference in the XAML
Also remember that if your enum is inside another class you need to use the + operator.
<Button CommandParameter="{x:Static local:MyOuterType+SearchPageType.First}".../>
You can use property element syntax instead of attribute syntax for this:
<Button x:Name="uxSearchButton"
Command="{Binding Path=SearchMembersCommand}"
Content="Search">
<Button.CommandParameter>
<SearchPageType>First</SearchPageType>
</Button.CommandParameter>
</Button>
Also if you want to provide a [Flags] enum you can use the property element syntax:
<Button>
<Button.CommandParameter>
<SearchPageType>First,Second</SearchPageType>
<Button.CommandParameter>
</Button>
Try this
CommandParameter="{x:Static "Class namespace e.g(Models)":SearchPageType.First}"
Related
Is there way to get name of property in XAML?
I found there is no support for nameof in XAML.
Something serving like this:
<i:InvokeCommandAction
Command="{Binding
Source={StaticResource SomeViewModel},
Path=SomeICommandImplementation}"
CommandParameter={Binding
Source={StaticResource SomeViewModel},
Path=SomeProperty,
GetNameOf=True}" />
Is there way to get string name of property in XAML.
No, there isn't. XAML is a markup language and it has no nameof operator defined.
What you could do is to try to implement your own custom InvokeCommandAction.
Create a class that derives from System.Windows.Interactivity.TriggerAction<DependencyObject>, add the properties of InvokeCommandAction (it is sealed so you cannot derive from it) and another GetNameOf property to it and then override the Invoke method to use the nameof operator.
I have a UserControl with some InputBindings. I wanted to make one of the input bindings (arrow key press) execute a command on a GUI control in my UserControl . So
e.g.
<UserControl.InputBindings>
<KeyBinding Key="Up" Command="{Binding ElementName=MyViewElement, Path=MoveUpCommand}"/>
<KeyBinding Key="Down" Command="{Binding ElementName=MyViewElement, Path=MoveDownCommand}"/>
</UserControl.InputBindings>
But, this fails because MyViewElement is not found because I assume it is declared later in the XAML. If I move my InputBindings to the end of the XAML file everything works as intended.
I kinda prefer my InputBindings to be at the top, is it possible to make it ignore the declaration order?
#Stewbob What are you talking about?
Take a look at this: http://msdn.microsoft.com/en-us/library/ms752308.aspx
<Window.CommandBindings>
<CommandBinding Command="ApplicationCommands.Open"
Executed="OpenCmdExecuted"
CanExecute="OpenCmdCanExecute"/>
</Window.CommandBindings>
According to what you said this should never work properly but it does:
<StackPanel>
<Menu>
<MenuItem Command="ApplicationCommands.Paste"
CommandTarget="{Binding ElementName=mainTextBox}" />
</Menu>
<TextBox Name="mainTextBox"/>
</StackPanel>
From what you said the binding comes first also it will be executed first and therefore the binding to mainTextBox should never work. Thats very not true.
Sure you can do everything in XAML, including such Binding at Window level. Show more code of your XAML/MyViewElement. Maybe we could tell you how to do it without errors. Also post your error here please.
But, this fails because MyViewElement is not found because I assume it is declared later in the XAML.
To avoid that, you can use the following in the code behind instead of the constructor:
protected override void OnInitialized(EventArgs e)
{
SomeCommand = new DelegateCommand(SomeExecuteMethod);
InitializeComponent();
base.OnInitialized(e);
}
This ensures the commands are already instantiated before you use them with:
Command="{Binding ElementName=MyUserCOntrol, Path=SomeCommand}"
hi in my silverlight5 and mvvm pattern,
i had one mainpage that page contains one button,
<Button Command="MyCommand"> Click Me </Button>
This above error can occur when binding a Command inside a Xaml file.
Why this error coming.. Any Help..
have you tried
<Button Command="{Binding MyCommand}"> Click Me </Button>
Solution is,
In fact, the problem with this syntax reside in the way we call the command. The previous example were not using the Binding syntax.
i used the Binding keyword.
<Button Command="{Binding MyCommand}"> Click Me </Button>
Of course, you need bind to a ICommand property to the desired command.
public ICommand MyCommand
{
get {
return ....
}
}
How do I pass True to a CommandParameter?
Currently I am imperatively adding Boolean.True to the resource dictionary, but that seems like a clumsy way to do it.
Because command parameters are of type 'object' the XAML parser is unable to perform type conversion for you. If you pass 'true', the parser has no way of knowing that you want this converted to a boolean value. You will have to do this explicitly. You could use the property element syntax:
<Button>
<Button.CommandParameter>
<sys:Boolean>true</sys:Boolean>
</Button.CommandParameter>
</Button>
Where the sys namepsace is mapped:
xmlns:sys="clr-namespace:System;assembly=mscorlib"
ColinE's answer is fine, but I think it's a bit neater to define the true/false as resources. You only have to do that once:
<UserControl.Resources>
<sys:Boolean x:Key="BoolTrue">True</sys:Boolean>
<sys:Boolean x:Key="BoolFalse">False</sys:Boolean>
</UserControl.Resources>
Then you can reference it as a StaticResource for the CommandParameter:
<Button CommandParameter="{StaticResource BoolTrue}" />
your XAML changes to this.
<Button
Command="{Binding Path=WhateverCommand}"
CommandParameter="{x:Static BooleanHelper.True}" />
I'm working with the MVVM pattern in WPF (a bit new to both).
I'd like to set up an InputBinding on a CheckBox that corresponds to a Control + Click event, but do not see a Modifiers property on the MouseBinding element. This is what I'd like to achieve (fictitious code, obviously- Modifiers doesn't exist):
<CheckBox>
<CheckBox.InputBindings>
<MouseBinding MouseAction="LeftClick"
Command="{Binding CheckboxControlClickCommand}"
Modifiers="Control" />
</CheckBox.InputBindings>
</CheckBox>
Any ideas on how to accomplish this without using events?
Thanks!
Use it with keybinding too!
An old question but looks like the MouseBinding now provides a Gesture attribute just for this..
<CheckBox>
<CheckBox.InputBindings>
<MouseBinding Gesture="CTRL+LeftClick"
Command="{Binding CheckboxControlClickCommand}"/>
</CheckBox.InputBindings>
</CheckBox>
I ended up using Keyboard.Modifiers in the Execute() context of the ICommand, which seemed to work just fine.
if (Keyboard.Modifiers != ModifierKeys.Control) return;
...
I think a behavior would do the trick. You can take a look at this link.