2012-10-04 124 views
2

例如我有一类人:WPF数据绑定对象

class Person { 
    string Name { get; set; } 
} 

在我的我的WPF应用程序主窗口的我得到这个类的ObservableCollection,我想在一个表中显示这一点。 我得到它的工作,以显示在一个表中的成员(只在这种情况下的名称),但由于某些原因,我在表中的按钮。在这个按钮中,我使用xaml文件中的'command'属性将ICommand绑定到它。

这就是我得到的工作sofar,但我怎么能发送对象'人'本身作为参数的命令?我用xaml中的命令参数成功地为字符串/ int,但我怎么能发送我在行中显示的对象?

Thnx!

+2

你应该能够只通过目标ct本身作为'CommandParameter'使用'CommandParameter =“{绑定}” – Rachel

+0

thnx去试试吧;) 它的工作!你是伟大的:D –

+2

重要的是你明白为什么工作......理解绑定, 该特定行的DataContext是一个人所以{绑定}是{Binding Path = DataContext}的简称} –

回答

2
<Button Command="{Binding ElementName=}" CommandParameter={Binding ElementName=ObjectToSendWithCommand" />` 

听起来你不是在CommandParameter值正确绑定的对象。我需要在代码隐藏或ViewModel中使用公共属性(如果您使用的是MVVM),并且如果此值发生更改,则需要使用PropertyChanged事件。

希望这是有道理的。

0

我试图做同样的事情,但我没有找到任何方式创建Binding到另一个绑定。我该如何解决这个问题:

我做在同一个地方SelectedItem财产,其中ICommand是,那么我收藏的所选项目绑定到我的SelectedItem财产,mode=TwoWay。通过这种方式,我总是在我的SelectedItem属性中收集所选项目,并且在ICommand中我可以访问它。

希望这有助于你解决你的问题......

0

您XAML文件看起来像

<ListBox ItemsSource="{Binding MultipleCopyList, Mode=TwoWay}"> 
<ListBox.ItemTemplate> 
<DataTemplate> 
     <StackPanel Orientation="Horizontal"> 
     <TextBlock Text="{Binding HeaderText,Mode=TwoWay}" Grid.Column="1" /> 
     </StackPanel> 
    </DataTemplate> 
</ListBox> 

你触发模样

<i:Interaction.Triggers> 
<i:EventTrigger EventName="MouseLeftButtonUp"> 


<cmd:EventToCommand Command="{Binding DataContext.MouseClickCommand, RelativeSource={RelativeSource AncestorType=ListBox}}" CommandParameter ="{Binding}" /> 
</i:EventTrigger> 
</i:Interaction.Triggers> 

和您的视图模型样子

private RelayCommand<StateForm> _MouseClickCommand; 
    public RelayCommand<StateForm> MouseClickCommand 
    { 
     get { 
      if (_MouseClickCommand == null) 
      { 
       _MouseClickCommand = new RelayCommand<StateForm>(e => MouseClick(e)); 
      } 
      return _MouseClickCommand; } 
     set 
     { 
      _MouseClickCommand = value; 
      RaisePropertyChanged("MouseClickCommand"); 
     } 
    } 
    private void MouseClick(StateForm e) 
    { 
    Your Code goes Here 
     }