Wpf 从xaml绑定代码隐藏方法
我在我的新项目中学习wpf,并为游戏应用程序开发用户控制 遗憾的是,我在将下面代码中的Wpf 从xaml绑定代码隐藏方法,wpf,Wpf,我在我的新项目中学习wpf,并为游戏应用程序开发用户控制 遗憾的是,我在将下面代码中的behind方法绑定到按钮命令属性时遇到了问题 这是我的密码: 代码隐藏: [Command] public void OnButtonClick(object param) { if (this.CustomClick != null) this.CustomClick(this); } <ItemsControl x:Name="itemsctrlCe
behind
方法绑定到按钮命令
属性时遇到了问题
这是我的密码:
代码隐藏:
[Command]
public void OnButtonClick(object param)
{
if (this.CustomClick != null)
this.CustomClick(this);
}
<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>
Xaml:
[Command]
public void OnButtonClick(object param)
{
if (this.CustomClick != null)
this.CustomClick(this);
}
<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
方法绑定到button命令属性?C#code是这个xaml文件的源代码?如果是,为什么要绑定?当您进行绑定时,您绑定到的是一个属性,而不是一个函数。您应该将命令绑定到ICommand proprety。以下是我的工作:
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();
}
}
函数_execute是将被触发的函数,另一个函数允许您检查是否希望函数_execute被执行
在您的Xaml中:
<Button Grid.Row="2" Content="Connexion" Command="{Binding Inscription}"></Button>
但是如果你不做MVVM,你应该不用绑定就直接使用代码。我想,我也不是专家。哈哈。C#代码就是这个xaml文件的源代码?如果是,为什么要绑定?当您进行绑定时,您绑定到的是一个属性,而不是一个函数。您应该将命令绑定到ICommand proprety。以下是我的工作:
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();
}
}
函数_execute是将被触发的函数,另一个函数允许您检查是否希望函数_execute被执行
在您的Xaml中:
<Button Grid.Row="2" Content="Connexion" Command="{Binding Inscription}"></Button>
但是如果你不做MVVM,你应该不用绑定就直接使用代码。我想,我也不是专家。哈哈。C#代码就是这个xaml文件的源代码?如果是,为什么要绑定?当您进行绑定时,您绑定到的是一个属性,而不是一个函数。您应该将命令绑定到ICommand proprety。以下是我的工作:
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();
}
}
函数_execute是将被触发的函数,另一个函数允许您检查是否希望函数_execute被执行
在您的Xaml中:
<Button Grid.Row="2" Content="Connexion" Command="{Binding Inscription}"></Button>
但是如果你不做MVVM,你应该不用绑定就直接使用代码。我想,我也不是专家。哈哈。C#代码就是这个xaml文件的源代码?如果是,为什么要绑定?当您进行绑定时,您绑定到的是一个属性,而不是一个函数。您应该将命令绑定到ICommand proprety。以下是我的工作:
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();
}
}
函数_execute是将被触发的函数,另一个函数允许您检查是否希望函数_execute被执行
在您的Xaml中:
<Button Grid.Row="2" Content="Connexion" Command="{Binding Inscription}"></Button>
但是如果你不做MVVM,你应该不用绑定就直接使用代码,我想,我不是专家,哈哈。试着创建命令绑定
在.xaml中:
<Window.Resources>
<RoutedUICommand x:Key="CloseCommand" Text=""/>
</Window.Resources>
<Window.Commandbindings>
<CommandBinding Command="{StaticResource CloseCommand}" Executed="Close" />
</Window.Commandbindings>
public MainConstructor()
{
InitializeComponent();
DataContext = this;
}
public void Close(Object sender, ExecutedRoutedEventArgs e)
{
// Stuff that happens in your `Close command`
}
您的.cs:
<Window.Resources>
<RoutedUICommand x:Key="CloseCommand" Text=""/>
</Window.Resources>
<Window.Commandbindings>
<CommandBinding Command="{StaticResource CloseCommand}" Executed="Close" />
</Window.Commandbindings>
public MainConstructor()
{
InitializeComponent();
DataContext = this;
}
public void Close(Object sender, ExecutedRoutedEventArgs e)
{
// Stuff that happens in your `Close command`
}
尝试创建命令绑定
在.xaml中:
<Window.Resources>
<RoutedUICommand x:Key="CloseCommand" Text=""/>
</Window.Resources>
<Window.Commandbindings>
<CommandBinding Command="{StaticResource CloseCommand}" Executed="Close" />
</Window.Commandbindings>
public MainConstructor()
{
InitializeComponent();
DataContext = this;
}
public void Close(Object sender, ExecutedRoutedEventArgs e)
{
// Stuff that happens in your `Close command`
}
您的.cs:
<Window.Resources>
<RoutedUICommand x:Key="CloseCommand" Text=""/>
</Window.Resources>
<Window.Commandbindings>
<CommandBinding Command="{StaticResource CloseCommand}" Executed="Close" />
</Window.Commandbindings>
public MainConstructor()
{
InitializeComponent();
DataContext = this;
}
public void Close(Object sender, ExecutedRoutedEventArgs e)
{
// Stuff that happens in your `Close command`
}
尝试创建命令绑定
在.xaml中:
<Window.Resources>
<RoutedUICommand x:Key="CloseCommand" Text=""/>
</Window.Resources>
<Window.Commandbindings>
<CommandBinding Command="{StaticResource CloseCommand}" Executed="Close" />
</Window.Commandbindings>
public MainConstructor()
{
InitializeComponent();
DataContext = this;
}
public void Close(Object sender, ExecutedRoutedEventArgs e)
{
// Stuff that happens in your `Close command`
}
您的.cs:
<Window.Resources>
<RoutedUICommand x:Key="CloseCommand" Text=""/>
</Window.Resources>
<Window.Commandbindings>
<CommandBinding Command="{StaticResource CloseCommand}" Executed="Close" />
</Window.Commandbindings>
public MainConstructor()
{
InitializeComponent();
DataContext = this;
}
public void Close(Object sender, ExecutedRoutedEventArgs e)
{
// Stuff that happens in your `Close command`
}
尝试创建命令绑定
在.xaml中:
<Window.Resources>
<RoutedUICommand x:Key="CloseCommand" Text=""/>
</Window.Resources>
<Window.Commandbindings>
<CommandBinding Command="{StaticResource CloseCommand}" Executed="Close" />
</Window.Commandbindings>
public MainConstructor()
{
InitializeComponent();
DataContext = this;
}
public void Close(Object sender, ExecutedRoutedEventArgs e)
{
// Stuff that happens in your `Close command`
}
您的.cs:
<Window.Resources>
<RoutedUICommand x:Key="CloseCommand" Text=""/>
</Window.Resources>
<Window.Commandbindings>
<CommandBinding Command="{StaticResource CloseCommand}" Executed="Close" />
</Window.Commandbindings>
public MainConstructor()
{
InitializeComponent();
DataContext = this;
}
public void Close(Object sender, ExecutedRoutedEventArgs e)
{
// Stuff that happens in your `Close command`
}
欢迎来到堆栈溢出!我对你的文章做了一些编辑,下面是我所做的:我修正了你文章中的一些语法和拼写错误。此外,我还替换了底部的“请帮助”信息。通常,堆栈溢出不喜欢在问题的底部显示“谢谢”或“请帮助我”消息。另外,如果你有方法名,试着用`字符来包装它们,这样它们会更突出。有关格式设置的详细信息,请参阅。祝你好运!:)我以前从未见过[Command]
装饰程序,也许你应该看看它,以便为你清理一些东西。你是否在类构造函数中设置了DataContext=this
?欢迎使用堆栈溢出!我对你的文章做了一些编辑,下面是我所做的:我修正了你文章中的一些语法和拼写错误。此外,我还替换了底部的“请帮助”信息。通常,堆栈溢出不喜欢在问题的底部显示“谢谢”或“请帮助我”消息。另外,如果你有方法名,试着用`字符来包装它们,这样它们会更突出。有关格式设置的详细信息,请参阅。祝你好运!:)我以前从未见过[Command]
装饰程序,也许你应该看看它,以便为你清理一些东西。你是否在类构造函数中设置了DataContext=this
?欢迎使用堆栈溢出!我对你的文章做了一些编辑,下面是我所做的:我修正了你文章中的一些语法和拼写错误。此外,我还替换了底部的“请帮助”信息。通常,堆栈溢出不喜欢在问题的底部显示“谢谢”或“请帮助我”消息。另外,如果你有方法名,试着用`字符来包装它们,这样它们会更突出。有关格式设置的详细信息,请参阅。祝你好运!:)我以前从未见过[Command]
装饰程序,也许你应该看看它,以便为你清理一些东西。你是否在类构造函数中设置了DataContext=this
?欢迎使用堆栈溢出!我对你的文章做了一些编辑,下面是我所做的:我修正了你文章中的一些语法和拼写错误。此外,我还替换了底部的“请帮助”信息。通常,堆栈溢出不喜欢在问题的底部显示“谢谢”或“请帮助我”消息。另外,如果你有方法名,试着用`字符来包装它们,这样它们会更突出。有关格式设置的详细信息,请参阅。祝你好运!:)我以前从未见过[Command]
装饰程序,也许你应该看看它来帮你清理一下。你在yo中设置了DataContext=this