xamlmvvmdata-bindingwin-universal-apptemplate10

UWP InvokeCommandAction on TextBox inside ListView MVVM


i have a problem to bind a command in a usercontrol included in a view.

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;
        }
    }

So, i'd like to call QSoldChanged, on TextChanged event, but nothing happens. What am i missing? Is that the right way to set command in this case? If you need more just ask! Thanks in advance


Solution

  • Problem is with x:Bind of textbox . It's mode is One way. So text changes doesn't reflect on view model. Change it to Binding. And this also might be causing problem

    {x:Bind} does not use the DataContext as a default source—instead, it uses the page or user control itself. So it will look in the code-behind of your page or user control for properties, fields, and methods. To expose your view model to {x:Bind}, you will typically want to add new fields or properties to the code behind for your page or user control. Refer this link x:Bind

    hope it works