2016-03-25 55 views
0

我有一個問題來綁定包含在視圖中的用戶控件中的命令。UWP InvokeCommandAction在ListView MVVM內的TextBox

UserControl.xaml

<DataTemplate x:Key="SoldItemDataTemplate" x:DataType="data:SoldItem"> 
    <Grid HorizontalAlignment="Stretch"> 
     <Grid.ColumnDefinitions> 
     ... 
     </Grid.ColumnDefinitions> 
     ... 
      <TextBox Grid.Column="2" Text="{x:Bind q}"> 
       <Interactivity:Interaction.Behaviors> 
        <Core:EventTriggerBehavior EventName="TextChanged"> 
         <Core:InvokeCommandAction Command="{Binding Path=DataContext.QChanged, ElementName=SoldRows}" CommandParameter="{Binding Mode=TwoWay}"/> 
        </Core:EventTriggerBehavior> 
       </Interactivity:Interaction.Behaviors> 
      </TextBox> 
     </Grid> 
</DataTemplate> 
... 
<ListView x:Name="SoldRows" 
    ItemsSource="{Binding SoldItemsList , Mode=TwoWay}" 
    IsItemClickEnabled="True" 
    ItemTemplate="{StaticResource SoldItemDataTemplate}"> 
... 

MainPage.xaml中

<Page.DataContext> 
    <vm:MainPageViewModel x:Name="ViewModel"></vm:MainPageViewModel> 
</Page.DataContext> 
... 
<controls:UserControl Grid.Row="1"></controls:UserControl> 
... 

MainPageViewModel.cs

... 
private DelegateCommand<object> _QSoldChanged; 
... 
public DelegateCommand<object> QSoldChanged 
    { 
     get 
     { 
      if (_QSoldChanged == null) 
      { 
       _QSoldChanged = new DelegateCommand<object>((object par) => 
       { 
        ... 
       }); 
      } 

      return _QSoldChanged; 
     } 
    } 

所以,我想打電話QSoldChanged,在TextChanged事件,但沒有發生。我錯過了什麼?這是在這種情況下設置命令的正確方法嗎? 如果您需要更多隻是問! 在此先感謝

+0

問題是X:文本框的綁定。它的模式是一種方式。所以文本更改不反映視圖模型。將其更改爲綁定。希望它有效 – Archana

回答

0

問題與x:文本框的綁定。它的模式是一種方式。所以文本更改不反映視圖模型。將其更改爲綁定。 這也可能導致問題

{x:Bind}不使用DataContext作爲默認源代碼,而是使用頁面或用戶控件本身。所以它會查看頁面的代碼隱藏或用戶控件的屬性,字段和方法。要將您的視圖模型展示給{x:Bind},您通常會希望將新的字段或屬性添加到您的頁面或用戶控件背後的代碼中。 請參閱此鏈接x:Bind

希望工程

+0

你是對的!謝謝你的幫助 – thepasto