Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Friday, September 28, 2012 1:14 PM
I've tried based on posted suggestions to get an ENUM value to work with Command Parameters. The majority of posts show that the Control just needs an XMLNS reference to the class and then to do something like this:
http://stackoverflow.com/questions/359699/passing-an-enum-value-as-command-parameter-from-xaml
or
http://stackoverflow.com/questions/5745642/wpf-passing-enum-value-as-a-command-parameter
Now, ENUMs can be declared at the Namespace layer as well as at the Class layer, I tried both layers and failed when trying to bind like this:
CommandParameter="{Binding Path={x:Static local:TestEnum.First}}"
What I don't understand is in the example above is the TESTENUM a Namespace layer or Class Layer declaration. I tried both layers and just cannot get the binding to work.
Any suggestions, hints?
JP Cowboy Coders Unite!
All replies (3)
Friday, September 28, 2012 2:27 PM âś…Answered
The enumeration need to be outside any class. For example:
You have a Wpf Application called WpfApplication1, in your XAML code of your MainWindow:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1"
Title="WpfApplication1" Width="640" Height="480">
<Window.CommandBindings>
<CommandBinding Command="Close" CanExecute="OnCommandCanExecute" Executed="OnCommandExecuted"/>
</Window.CommandBindings>
<DockPanel>
<Button Command="Close" CommandParameter="{x:Static local:TestEnum.First}">Click me!</Button>
</DockPanel>
</Window>
And in the code behind:
using System.Windows;
using System.Windows.Input;
namespace WpfApplication1
{
public partial class MainWindow : Window
{
public MainWindow()
{
this.InitializeComponent();
}
private void OnCommandCanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = true;
}
private void OnCommandExecuted(object sender, ExecutedRoutedEventArgs e)
{
MessageBox.Show(this, e.Parameter.ToString(), "WpfApplication1");
}
}
public enum TestEnum
{
First
}
}
Hope this helps,
Miguel.
Friday, September 28, 2012 1:23 PM | 1 vote
Hello, you only need:
CommandParameter="{x:Static local:TestEnum.First}"
Regards,
Miguel.
Friday, September 28, 2012 1:39 PM
Hello Miguel;
I tried that and the error message I received was the the reference could not be found. I move the definitions around in the project thinking ok it just can't find it because it's in a differernt folder/namespace. I put it right in the code of the control itself as a class declared ENUM (didn't work), I created a new class in same folder as View (didn't work). I moved the declarations to namespace layer (didn't work), I tried declaring the ENUM static (did't work). I even tried something like this:
CommandParameter="{x:Static local:ClassName+TestEnum.First}" <(Didn't work)
JP Cowboy Coders Unite!