如何将当前窗口作为参数传递给命令?
我喜欢在XAML-markup中执行此操作:
<Button Command="CommandGetsCalled" CommandParameter="-this?-" />
Run Code Online (Sandbox Code Playgroud) 我想将Xamarin.Forms.Button
它自己Command
作为CommandParameter
我的ViewModel 传递给它.我知道如何从背后的代码实现这一点,例如......
XAML (由于简洁而遗漏了大多数属性)
<Button x:Name="myButton"
Text="My Button"
Command="{Binding ButtonClickCommand}"/>
Run Code Online (Sandbox Code Playgroud)
XAML.cs
public partial class MyTestPage
{
public MyTestPage()
{
InitializeComponent();
myButton.CommandParameter = myButton;
}
}
Run Code Online (Sandbox Code Playgroud)
视图模型
public class MyViewModel : ViewModelBase
{
public MyViewModel()
{
ButtonClickCommand = new Command(
(parameter) =>
{
var view = parameter as Xamarin.Forms.Button;
if (view != null)
{
// Do Stuff
}
});
}
public ICommand ButtonClickCommand { get; private set; }
}
Run Code Online (Sandbox Code Playgroud)
......但是可以CommandParameter
在XAML中声明它吗?或者换句话说,将参数设置为按钮本身的绑定语法是什么?
<Button x:Name="myButton"
Text="My …
Run Code Online (Sandbox Code Playgroud) 我有一个DataTemplate代表AppBar按钮,我通过自定义AppBarCommand对象的集合声明.
public AppBarCommand(RelayCommand command, string buttonstyle)
{
Command = command;
ButtonStyle = buttonstyle;
}
<DataTemplate>
<Button Command="{Binding Command}"
Style="{Binding ButtonStyle, Converter={StaticResource StringNameToStyleConverter}}"/>
</DataTemplate>
Run Code Online (Sandbox Code Playgroud)
我想添加一个CommandParameter绑定,但参数必须是Button本身.这样我就可以设置Callisto弹出窗口的PlacementTarget.这可能吗?
MVVM-Light Toolkit
在Silverlight 5中使用,我试图找到一种方法在事件到命令行为中将Command Parameters
AND 传递EventArgs
给ViewModel.
我没有找到一个职位提示路过的EventArgs作为命令参数,但在我的情况,我想用EventArgs
和Command Parameter
无论是在视图模型.
有人可以帮忙吗?
如何正确绑定动态创建的菜单项列表.我尝试了几件事,但似乎都没有效果.我得到了正确的名单,但我的ViewSwitchCommand似乎没有正确启动.
<MenuItem Foreground="White" Header="Names" ItemsSource="{Binding Player.ToonNames}" Command="{Binding ViewSwitchCommand}" CommandParameter="{Binding Header}"/>
Run Code Online (Sandbox Code Playgroud)
但是,如果我不动态地这样做,那么一切正常,可以让它工作
<MenuItem Foreground="White" Header="Names">
<MenuItem Foreground="Black" Header="Chat" Command="{Binding ViewSwitchCommand}" CommandParameter="player1" />
<MenuItem Foreground="Black" Header="Craft" Command="{Binding ViewSwitchCommand}" CommandParameter="player2" />
</MenuItem>
Run Code Online (Sandbox Code Playgroud)
命令参数需要一个字符串..不确定是不是它...希望这是一个简单的我只是忽略
我正在尝试将ViewModel中的变量作为参数发送到命令.该命令如下所示:
public class EditPersonCommand : ICommand
{
private bool _CanExecute = false;
public bool CanExecute(object parameter)
{
PersonModel p = parameter as PersonModel;
CanExecuteProperty = (p != null) && (p.Age > 0);
return CanExecuteProperty;
}
public event EventHandler CanExecuteChanged;
public void Execute(object parameter) { }
private bool CanExecuteProperty
{
get { return _CanExecute; }
set
{
if (_CanExecute != value)
{
_CanExecute = value;
EventHandler can_execute = CanExecuteChanged;
if (can_execute != null)
{
can_execute.Invoke(this, EventArgs.Empty);
}
}
}
}
} …
Run Code Online (Sandbox Code Playgroud) 我试图在WPF应用程序中使用Command和CommandParameter与Buttons绑定.我有这个完全相同的代码在Silverlight中工作正常,所以我想知道我做错了什么!
我有一个组合框和一个按钮,其中命令参数绑定到组合框SelectedItem:
<Window x:Class="WPFCommandBindingProblem.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<StackPanel Orientation="Horizontal">
<ComboBox x:Name="combo" VerticalAlignment="Top" />
<Button Content="Do Something" Command="{Binding Path=TestCommand}"
CommandParameter="{Binding Path=SelectedItem, ElementName=combo}"
VerticalAlignment="Top"/>
</StackPanel>
</Window>
Run Code Online (Sandbox Code Playgroud)
背后的代码如下:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
combo.ItemsSource = new List<string>(){
"One", "Two", "Three", "Four", "Five"
};
this.DataContext = this;
}
public TestCommand TestCommand
{
get
{
return new TestCommand();
}
}
}
public class TestCommand : ICommand
{
public bool CanExecute(object parameter)
{
return parameter is string && …
Run Code Online (Sandbox Code Playgroud) 我有一个特定的wpf问题.我正在尝试从Datagrid中删除一行,方法是定义一个Keybinding,它将Datagrid的选定行作为CommandParameter传递给Command.
这是我的Keybinding:
<UserControl.Resources >
<Commands:CommandReference x:Key="deleteKey" Command="{Binding DeleteSelectedCommand}"/>
</UserControl.Resources>
<UserControl.InputBindings>
<KeyBinding Key="D" Modifiers="Control" Command="{StaticResource deleteKey}"/>
</UserControl.InputBindings>
Run Code Online (Sandbox Code Playgroud)
我知道这基本上有效,因为我可以调试到DeleteSelectedCommand.但是有一个异常,因为DeleteSelectedCommand会将Datagrid的一行预先删除为Call Parameter.
如何通过Keybinding传递SelectedRow?
我想在可能的情况下仅在XAML中执行此操作,而不更改Code Behind.
我怎么True
转到CommandParameter
?
目前我势在必须添加Boolean.True
到资源字典中,但这似乎是一种笨拙的方式.
我在想如果windows phone 7按钮事件类似于使用C#的ASP.NET开发,类似于按钮,我在XAML中将值设置为commandparameter,在后面的代码中,我得到命令参数并重定向页面.
我没有找到任何关于按钮事件处理的好例子,有什么建议吗?
谢谢.
commandparameter ×10
wpf ×6
c# ×4
binding ×3
command ×3
mvvm ×2
silverlight ×2
xaml ×2
button ×1
data-binding ×1
datatemplate ×1
key-bindings ×1
menuitem ×1
mvvm-light ×1
parameters ×1