2012-07-25 125 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設置綁定中綁定的控件。