picpic2006
Topic Author
Posts: 71
Joined: 07 Nov 2013, 15:59

button binding command with parameter

28 Apr 2014, 09:16

Hi i tried to make a custom command that can throw a parameter " a string".

Your tutorial don't throw a parameter, i actually search on google i found some documentation, i try but
i really don't see how make relation with your code.

i must admit it is a really new concept for me !

Thanks you very much
 
wckdspn
Posts: 67
Joined: 18 Aug 2012, 23:14

Re: button binding command with parameter

28 Apr 2014, 19:07

In addition to the Command property, there is also a CommandParameter property that you can bind.
 
picpic2006
Topic Author
Posts: 71
Joined: 07 Nov 2013, 15:59

Re: button binding command with parameter

28 Apr 2014, 20:42

thanks for the reply.

I saw that parameter, my question is about the code behind this in c# with unity.

i don't know how to expose this parameter in my command.
I actually read somes exemples on the web, but i tryed some configs, but none works.

Thanks
 
wckdspn
Posts: 67
Joined: 18 Aug 2012, 23:14

Re: button binding command with parameter

28 Apr 2014, 21:13

Exactly what do you want? How are you exposing the command? Are you binding in code or in the XAML? The parameter is just another object property, you can bind it to just about anything.
 
picpic2006
Topic Author
Posts: 71
Joined: 07 Nov 2013, 15:59

Re: button binding command with parameter

28 Apr 2014, 21:31

Sorry my project with my code is at my work but i can explain what i wan't to acheive more precisely.

I have made a command with no parameter. and bind it and expose it outside my usercontrol.
"SelectBoutonCommand"

in my ui code i made this.

CustomButton btn = new CustomButton();
btn.selectBoutonCommand = new SelectedBoutonCommand(selectbouton);

public void selectBouton()
{
Here i wan't to get my clicked bouton name to throw a task by context.
}

I have read some exemple. but in noesis command, the parameter is a base component.
All exemples i read from xaml the parameter is an object.

I really misunderstand some thing, i have to read more !
 
User avatar
sfernandez
Site Admin
Posts: 3222
Joined: 22 Dec 2011, 19:20

Re: button binding command with parameter

29 Apr 2014, 11:10

Hi,

As wckdspn indicated, you should be able to bind the CommandParameter property to almost anything in your UI design.

The parameter is not used by our DelegateCommand class, but you can create your own command class that handles this parameter. For example:
[Noesis.Extended]
public class MyCommand : Noesis.BaseCommand
{
    private readonly Action<Noesis.BaseComponent> action;

    public MyCommand(Action<Noesis.BaseComponent> action)
    {
        this.action = action;
    }

    public bool CanExecuteCommand(Noesis.BaseComponent parameter)
    {
        return parameter != null;
    }

    public void ExecuteCommand(Noesis.BaseComponent parameter)
    {
        action(parameter);
    }
}
Your View Model would expose the command the same as in our Commands example:
[Noesis.Extended]
public class ViewModel : Noesis.SerializableComponent
{
    string _input = string.Empty;
    public string Input 
    { 
        get
        {
            return _input;
        }
        set
        {
            _input = value; 
        }
    }

    private string _output = string.Empty;
    public string Output
    {
        get
        {
            return _output;
        }
        set
        {
            if (_output != value)
            {
                _output = value;
                NotifyPropertyChanged("Output");
            }
        }
    }

    public MyCommand SayHelloCommand 
    { 
        get;
        private set;
    }

    public ViewModel()
    {
        SayHelloCommand = new MyCommand(SayHello);
    }

    private void SayHello(Noesis.BaseComponent parameter)
    {
        Output = System.String.Format("[{0}] Hello, {1}", parameter.AsString(), Input);
    }
}
And you specify the command parameter the way you want, for example by binding to a TextBox.Text:
<Grid
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:noesis="clr-namespace:Noesis.Samples">
    
    <Grid.Resources>
        <noesis:ViewModel x:Key="ViewModel"/>
    </Grid.Resources>

    <Viewbox>
    <Border x:Name="myBorder" DataContext="{StaticResource ViewModel}" Width="400" Margin="50"
        Background="#801C1F21" BorderThickness="1" CornerRadius="5" BorderBrush="#D0101611" Padding="5"
        HorizontalAlignment="Center" VerticalAlignment="Center">
        <StackPanel Orientation="Vertical">
            <TextBox MinWidth="300" Margin="3" Text="{Binding Input, Mode=TwoWay}" FontSize="28"/>
            <TextBox x:Name="Param" MinWidth="300" Margin="3" Text="1" FontSize="24"/>
            <Button Content="Say Hello" Margin="3" Command="{Binding SayHelloCommand}" CommandParameter="{Binding Text, ElementName=Param}" FontSize="28" />
            <Viewbox Margin="5" Height="50">
                <TextBlock Margin="5" Padding="0" TextAlignment="Center" Text="{Binding Output}" FontSize="28" Foreground="White"/>
            </Viewbox>
        </StackPanel>
    </Border>
    </Viewbox>
    
</Grid>
 
picpic2006
Topic Author
Posts: 71
Joined: 07 Nov 2013, 15:59

Re: button binding command with parameter

29 Apr 2014, 11:27

Thanks you very much,

I will try to reproduce it. I think it is a good idee to make this in the showcase exemples in forums.

You just save to me a lot of times, i tryed yesterday with this exemple and it was not working. I was close in off but i writed not correctly my command.

http://stackoverflow.com/questions/1728 ... er-in-xaml
 
picpic2006
Topic Author
Posts: 71
Joined: 07 Nov 2013, 15:59

Re: button binding command with parameter

29 Apr 2014, 14:13

Thanks it work very well, my problem is clearly my understood of c# part and xaml.

I have a long learning curve with noesis but i really love this plugin, it is clearly a great addition for unity.
I don't know today how the new gui system will be. I'm a little perplex.
But I will continue to work with noesis for sure !

Thanks again.
 
User avatar
jsantos
Site Admin
Posts: 4264
Joined: 20 Jan 2012, 17:18
Contact:

Re: button binding command with parameter

30 Apr 2014, 14:53

Thanks for your comments!

By the way, you are right, we should improve our documentation/samples to include this scenario.

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 5 guests