2012-07-25 79 views
2

我创建了一个包含ListView的用户控件。我想补充一个RelayCommand当用户(使用MVVM光)改变嵌套文本框的文本:DataTemplate中的MVVM Light命令

<UserControl xmlns:my="clr-namespace:UI.View" x:Class="UI.View.MontureView" 
      xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" 
       xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.WPF4" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      mc:Ignorable="d" > 
     <ListView ItemsSource="{Binding Path=Monture}" Margin="0,39,0,95" Height="600" HorizontalAlignment="Center"> 
      <ListView.View> 
       <GridView> 
        <GridViewColumn Header="Qte" Width="50" > 
         <GridViewColumn.CellTemplate > 
          <DataTemplate> 
           <TextBox Text="{Binding Path=Qte}" Width="40" TextAlignment="Right" Name="a"> 
            <i:Interaction.Triggers> 
             <i:EventTrigger EventName="TextChanged" > 
              <cmd:EventToCommand Command="{Binding MontureViewModel.MyProperty}" CommandParameter="{Binding ElementName=a}" /> 
             </i:EventTrigger> 
            </i:Interaction.Triggers> 
           </TextBox> 
          </DataTemplate> 
         </GridViewColumn.CellTemplate> 
        </GridViewColumn> 
       </GridView> 
      </ListView.View> 
     </ListView> 
</UserControl> 

在我的虚拟机,我有:(我删除了代码的某些部分)

namespace UI.ViewModel 
{ 
    public class MontureViewModel : ViewModelBase 
    { 
     public MontureViewModel() 
     { 
      MyProperty = new RelayCommand<TextBox>(e => 
      { 
       MessageBox.Show("test"); 
      }); 
     } 
     public RelayCommand<TextBox> MyProperty { get; set; } 
    } 
} 

我试图添加一个事件的文本框没有嵌套到DataTemplate(ListView之外),它的工作原理。 我认为我必须在进入DataTemplate时修改代码。

有什么想法?

+0

在调试输出窗口中是否有任何绑定错误?请检查它 – 2012-07-25 13:33:52

+0

我在调试输出窗口中没有任何错误。 – user1551721 2012-07-25 13:46:41

+0

在调试? – 2012-07-25 13:47:22

回答

0

您需要更改CommandParameter = “{绑定的ElementName = A}” 到

CommandParameter = “{结合的SelectedItem,的ElementName = A}”。这将CommandParameter绑定到GridView的选定项,并且ElementName设置绑定中绑定的控件。