wpf 按钮命令绑定
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19139839/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Button Command Binding
提问by DermFrench
I have a button inside a listbox. I want to bind the command to the DataContext of the Main Grid. I'm not sure who to do this, below is my attempt.
我在列表框中有一个按钮。我想将命令绑定到主网格的 DataContext。我不确定该由谁来执行此操作,以下是我的尝试。
I want to bind to ViewModel.SelectionEditorSelectionSelectedCommand on my view model, which the main grid is bound to, I don't want to bind to the actual filteredSelection.SelectionEditorSelectionSelectedCommand
我想绑定到主网格绑定到的视图模型上的 ViewModel.SelectionEditorSelectionSelectedCommand,我不想绑定到实际的 filtersSelection.SelectionEditorSelectionSelectedCommand
Here is my XAML
这是我的 XAML
<Grid Name="MainGrid">
.....
<ListBox x:Name="MarketsListBox" Height="Auto" MaxHeight="80" ItemsSource="{Binding Path=FilteredMarkets}" Margin="5" Width="Auto" HorizontalAlignment="Stretch"
>
ListBox.ItemTemplate>
<DataTemplate>
<WrapPanel Orientation="Horizontal">
<Button Height="Auto"
Content="{Binding FinishingPosition,Converter={StaticResource FinishingPositionToShortStringConverter1}}"
Foreground="{Binding Path=FinishingPosition, Converter={StaticResource FinishingPositionToColourConverter1}}"
Margin="2" Width="20"
Command="{Binding ElementName=MainGrid.DataContext, Path=SelectionEditorSelectionSelectedCommand}"
CommandParameter="{Binding}"
/>
.....
回答by nmclean
Binding to the grid using ElementNameshould work, but you have made a small error in the binding syntax. ElementNamemust include the name only, not a property. You simply need to include DataContextin the Path:
使用绑定到网格应该ElementName可以工作,但是您在绑定语法中犯了一个小错误。ElementName必须只包含名称,而不是属性。您只需要包含DataContext在Path:
Command="{Binding ElementName=MainGrid,
Path=DataContext.SelectionEditorSelectionSelectedCommand}"
回答by sircodesalot
So based on this line:
所以基于这一行:
Command="{Binding ElementName=MainGrid.DataContext ... }
I'm assuming you have something like this:
我假设你有这样的事情:
<Grid Name="MainGrid">
<Grid.DataContext>
<lol:GridViewModel /> <!--Some kind of view model of sorts-->
</Grid.DataContext>
... content
</Grid>
Then all you would have to do is on the ViewModel class create a public property that returns some sort of ICommand, such as:
然后您要做的就是在 ViewModel 类上创建一个返回某种类型的公共属性ICommand,例如:
class GridViewModel {
public ICommand SelectionEditorSelectionSelectedCommand {
get { return new TestCommand(); }
}
}
Where TestCommandwould be some kind of class implementing ICommandas in:
哪里TestCommand会实现某种类,ICommand如:
class TestCommand : ICommand {
public event EventHandler CanExecuteChanged { get; set; }
public bool CanExecute(object parameter)
{
return true; // Expresses whether the command is operable or disabled.
}
public void Execute(object parameter)
{
// The code to execute here when the command fires.
}
}
Basically, for ICommandyou just need to define what happens when the command Executes, how to determine whether or not it CanExecuteand then supply an event handle for when CanExecuteChanged. Once you have this setup, all you have to do is wire up your button like this:
基本上,因为ICommand您只需要定义 command 时会发生什么Executes,如何确定它是否CanExecute,然后为 when 提供事件句柄CanExecuteChanged。完成此设置后,您所要做的就是像这样连接按钮:
<Button Command="{Binding SelectionEditorSelectionSelectedCommand}" />
And that's it. Basically the binding will automatically check your ViewModel class for a property called SelectionEditorSelectionSelectedCommand, that implements ICommand. When it reads the property it will instantiate an instance of TestCommand, and WPF will handle it from there. When the button is clicked Executewill be fired like clockwork.
就是这样。基本上绑定会自动检查您的 ViewModel 类是否有一个名为 的属性SelectionEditorSelectionSelectedCommand,该属性实现了ICommand. 当它读取属性时,它将实例化 的一个实例TestCommand,WPF 将从那里处理它。当按钮被点击时,Execute会像发条一样被触发。
回答by Tico
You should try as I did in a similar situation:
您应该像我在类似情况下所做的那样尝试:
<Button Command="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type Grid}}, Path=DataContext.YOURCOMMANDHERE}" />
I had a button inside a TabItem Header and it worked Ok! The thing is, your Command is a Property of the DataContext, so your path should indicate it.
我在 TabItem Header 中有一个按钮,它工作正常!问题是,您的 Command 是 DataContext 的一个属性,因此您的路径应该指出它。
Good Luck!
祝你好运!
EDIT: Elementname might work as well.
编辑: Elementname 也可能有效。

