绑定来自xaml的方法背后的代码

时间:2015-03-06 01:29:40

标签: wpf

我正在我的新项目中学习wpf并正在为游戏应用程序开发用户控件。

可悲的是,我在将以下代码中的behind方法绑定到Button Command属性时遇到了问题。

这是我的代码:

代码隐藏:

[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>

如何将behind方法绑定到按钮命令属性?

2 个答案:

答案 0 :(得分:3)

尝试创建Command bindings

.xaml

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

您的Button

<Button Command={StaticResource CloseCommand}" />

您的 .cs

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

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

答案 1 :(得分:0)

C#代码是这个xaml文件的代码?如果是,为什么要绑定?当你进行绑定时,你绑定到一个proprety,而不是一个函数。您应该将命令绑定到ICommand属性。这是我的工作:

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();
    }
}

然后你创建一个你想绑定的ICommand proprety。

private ICommand inscription;

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

            return this.inscription;
        }
    }

function_execute是triger的函数,另一个函数允许你检查你是否想要执行你的function_execute。

在您的Xaml中:

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

但是如果你不做MVVM,你应该只使用背后的代码而没有绑定我认为,我也不是专家。