user4639144
user4639144

Reputation: 57

bind code behind method from xaml

I am learning wpf in my new project and am developing user control for game application.

Sadly, I'm facing problem in binding the behind method in the below code to the Button Command property.

Here is my code:

Codebehind:

[Command]
public void OnButtonClick(object param)
{
    if (this.CustomClick != null)
        this.CustomClick(this);            
}

Xaml:

<ItemsControl x:Name="itemsctrlCell" 
              HorizontalAlignment="Center"
              ItemsSource="{Binding  ElementName=userctrlGameBoard, Path= Dimension,Converter={StaticResource Convert}}" 
              Width="Auto"  
              Height="Auto" 
              Margin="77,92,-75.2,-92.4">

    <ItemsControl.Template>
        <ControlTemplate TargetType="ItemsControl">
            <Border  BorderThickness="1" 
                     CornerRadius="15" 
                     Height="Auto" 
                     Width="Auto">
                <ItemsPresenter  Width="Auto" 
                                 Height="Auto" 
                                 HorizontalAlignment="Center"/>
            </Border>
        </ControlTemplate>
    </ItemsControl.Template>
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <UniformGrid Width="Auto" Height="Auto"></UniformGrid>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <DataTemplate.Resources>
                <Style TargetType="TextBlock">
                    <Setter Property="FontSize" Value="18"/>
                    <Setter Property="HorizontalAlignment" Value="Center"/>
                </Style>
            </DataTemplate.Resources>
            <WrapPanel x:Name="wrppanelCell"  
                       Width="Auto"
                       Height="Auto">

                <Button Name="btnCell" 
                        Width="40" 
                        Height="40" 
                        FontSize="25" 
                        FontWeight="Bold" 
                        HorizontalContentAlignment="Right"
                        HorizontalAlignment="Center" 
                        Command="{Binding ElementName=userctrlGameBoard,Path=OnButtonClick}"
                        CommandParameter="{Binding}">
                </Button>
            </WrapPanel>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

How can I bind the behind method to the button command property?

Upvotes: 0

Views: 1956

Answers (2)

Sonhja
Sonhja

Reputation: 8448

Try this to create Command bindings.

In your .xaml:

<Window.Resources>
    <RoutedUICommand x:Key="CloseCommand" Text=""/>
</Window.Resources>
<Window.Commandbindings>
    <CommandBinding Command="{StaticResource CloseCommand}" Executed="Close" />
</Window.Commandbindings>

Your Button:

<Button Command={StaticResource CloseCommand}" />

Your .cs:

public MainConstructor()
{
    InitializeComponent();
    DataContext = this;
}

public void Close(Object sender, ExecutedRoutedEventArgs e)
{
    // Stuff that happens in your `Close command`
}

Upvotes: 3

king
king

Reputation: 507

The C# code is the code begind of this xaml file ? If yes why you want to bind ? When you do a Binding, you bind to a proprety, not to a function. You are supposed to bind your Command to a ICommand proprety. Here is what I do:

public class MyCommand : ICommand
{
    #region private fields
    private readonly Action execute;
    private readonly Func<bool> canExecute;
    #endregion

    public event EventHandler CanExecuteChanged;

    public MyCommand(Action execute)
        : this(execute, null)
    {
    }

    public MyCommand(Action execute, Func<bool> canExecute)
    {
        if (execute == null)
            throw new ArgumentNullException("execute");

        this.execute = execute;
        this.canExecute = canExecute;
    }

    public void Execute(object parameter)
    {
        this.execute();
    }

    public bool CanExecute(object parameter)
    {
        return this.canExecute == null ? true : this.canExecute();
    }
}

Then you creat a ICommand proprety that you want to bind.

private ICommand inscription;

    public ICommand Inscription
    {
        get
        {
            if (this.inscription == null)
                this.inscription = new MyCommand(function_execute, function_canExecute);

            return this.inscription;
        }
    }

The function_execute is the function that will be triger, this other one allow you to check if you want that your function_execute will be execute.

In your Xaml:

<Button Grid.Row="2" Content="Connexion" Command="{Binding Inscription}"></Button>

But if you don't do MVVM, you should just use the code behind without binding I think, I'm not an expert too lol.

Upvotes: 0

Related Questions