I have a dynamic list in the window, which I implement via ItemsControl I need to handle the events occurring in the TextBox in this list. More precisely, then auto-add a character after entering the first two and auto- TextBlock at the moment of losing focus. Tried to implement through EventTrigger , as advised on the English StackOverflow. There are no errors, but the debugger does not enter the commands. The program simply does not see them. The button works, the lines are added to the list, but the commands are not attached. What am I doing wrong and how can I handle events in dynamic control correctly?

Code: XAML

 <UserControl x:Class="PresentationLayer.Views.FLPStream.StartFlp03" 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" xmlns:local="clr-namespace:PresentationLayer.Views.FLPStream" xmlns:vms="clr-namespace:BusinessLayer.ViewModels.VM.SecondaryEntitiesVM;assembly=BusinessLayer" xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" mc:Ignorable="d" d:DesignHeight="700" d:DesignWidth="900"> <Grid Background="LightGray"> <Grid.RowDefinitions> <RowDefinition Height="auto"></RowDefinition> <RowDefinition Height="auto"></RowDefinition> <RowDefinition Height="*"></RowDefinition> <RowDefinition Height="auto"></RowDefinition> </Grid.RowDefinitions> <TextBlock Grid.Row="0" Margin="5" Text="Окно добавления видов деятлеьности. Нажмите кнопку Добавить для добавления вида деятельности. Количество видов ограничено 21" TextWrapping="Wrap" HorizontalAlignment="Center"> </TextBlock> <Button Grid.Row="1" Content="Добавить вид деятельности" Width="250" Height="auto" FontFamily="Arial" FontSize="16" FontWeight="Bold" Padding="10" Foreground="DarkGreen" Margin="10" Command="{Binding AddActivityCommand}" /> <ScrollViewer Grid.Row="2" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto"> <ItemsControl ItemsSource="{Binding Activities}" > <ItemsControl.ItemTemplate> <DataTemplate> <Grid HorizontalAlignment="Left"> <Grid.ColumnDefinitions> <ColumnDefinition Width="0.2*"></ColumnDefinition> <ColumnDefinition Width="0.8*"></ColumnDefinition> </Grid.ColumnDefinitions> <TextBox Name="code" Grid.Column="0" Width="50" Text="{Binding ActivityCode}" Margin="50,10"> <i:Interaction.Triggers> <i:EventTrigger EventName="TextChanged"> <i:InvokeCommandAction Command="{Binding ChangeTextCommand}"/> </i:EventTrigger> <i:EventTrigger EventName="LostFocus"> <i:InvokeCommandAction Command="{Binding LostFocusCommand}" CommandParameter="{Binding ActivityCode}" ></i:InvokeCommandAction> </i:EventTrigger> </i:Interaction.Triggers> </TextBox> <StackPanel Grid.Column="1" Margin="20,10"> <TextBlock Name="actName" Text="{Binding ActivityName}"/> <TextBlock Visibility="Collapsed" Name="ErrMessage"/> </StackPanel> </Grid> </DataTemplate> </ItemsControl.ItemTemplate> </ItemsControl> </ScrollViewer> <StackPanel Grid.Row="3" Orientation="Horizontal" Margin="15" HorizontalAlignment="Center"> <Button MinWidth="100" Margin="40,0,40,0" Command="{Binding BackCommand}"> <TextBlock Text="Назад" FontFamily="Arial" FontWeight="Bold" Margin="10"></TextBlock> </Button> <Button MinWidth="100" Margin="40,0,40,0" IsEnabled="{Binding RadiobuttonIsSelected}" Command="{Binding ForwardCommand}" > <TextBlock Text="Дальше" FontFamily="Arial" FontWeight="Bold" Foreground="Black" Margin="10" ></TextBlock> </Button> </StackPanel> </Grid> 

VM (part):

  public RelayCommand ForwardCommand { get; set; } public RelayCommand BackCommand { get; set; } public RelayCommand AddActivityCommand { get; set;} public RelayCommand ChangeTextCommand { get; set; } public RelayCommand LostFocusCommand { get; set; } public StartFLPVM3(MainVM mainPage) { BackCommand = new RelayCommand(x => BackMethod()); ForwardCommand = new RelayCommand(x => ForwardMethod()); ChangeTextCommand = new RelayCommand(x => ChangeTextMethod()); AddActivityCommand = new RelayCommand(x => AddActivityMethod()); LostFocusCommand = new RelayCommand(delegate (object s) { LostFocusMethod(s);}); Activities = new ObservableCollection<ActivityVM> (); } private void AddActivityMethod() { var activity = new ActivityVM(); Activities.Add (activity); } private void ChangeTextMethod() { //if(text.Length ==2) //{ // text += "."; //} } private void LostFocusMethod(object code) { } 

If necessary, you can take the implementation of the RelayCommand class here: link

    1 answer 1

    There is not enough ActivityVM code for a confident judgment, but judging by the above code, you hope that the command assigned to the primary VM will work for an object that is part of its collection property.

    Simply put, you need to move the ChangeTextCommand and LostFocusCommand to ActivityVM.

    • ActivityVM is a model cleared of database service fields. Only properties and constructor. She generally in BusinessLogic Layer. - foxhound