TextBox.Changed and select GridView

Alex Konkov 1 Reputation point
2022-07-07T13:54:30.307+00:00

Hi friends!
I'm a beginer, and i confuse.
This my xaml:

 <GridView  x:Name="gridView_goods"  
                ItemsSource="{x:Bind goods}"  
                SelectionMode="Multiple"     
                Grid.Column="1"  
                >  
        <GridView.ItemTemplate>  
            <DataTemplate x:Name="GoodTemplate" x:DataType="local:Good" >  
                <Grid AutomationProperties.Name = '{x:Bind idphoto}' Width = "Auto">  
                    <Grid.RowDefinitions>  
                        <RowDefinition Height="205"/>  
                        <RowDefinition Height="Auto"/>  
                    </Grid.RowDefinitions>  
                    <StackPanel Grid.Row="0">  
                        <Image Source = '{x:Bind uri}' Height = '200    ' Stretch = "Uniform" VerticalAlignment = 'Top'/>  
                    </StackPanel>  
                    <StackPanel  Height="Auto" Width="300" Margin = '8' Grid.Row="1" Orientation="Vertical">  
                        <TextBox x:Name="txtOriginal"  Text="{Binding txtOriginal}"  
                                         TextWrapping="Wrap"     
                                         AcceptsReturn="True"    
                                         Background="LightGray"  
                                         Height="AUTO"  
                                 IsReadOnly="True"  
                                         />  
                        <TextBox x:Name="txtModify" Text="{Binding txtModify, Mode=TwoWay}"  
                                         TextWrapping="Wrap"     
                                         AcceptsReturn="True"   
                                         TextChanged="txtModify_TextChanged"  
                                         ScrollViewer.VerticalScrollBarVisibility="Auto"  
                                         ScrollViewer.VerticalScrollMode="Auto"     
                                         Height="Auto"  
                                         />  
                    </StackPanel>  
                </Grid>  
            </DataTemplate>  
        </GridView.ItemTemplate>  
    </GridView>  

i want selected gridview row when i changed text in texyBox txtModify. How should I do it?

Universal Windows Platform (UWP)
{count} votes

1 answer

Sort by: Most helpful
  1. Nico Zhu (Shanghai Wicresoft Co,.Ltd.) 12,866 Reputation points
    2022-07-08T06:32:33.34+00:00

    i want selected gridview row when i changed text in texyBox txtModify.

    The easy way is using xaml Behaviors nuget to detect current TextChanged event and if it was triggered then call ChangePropertyAction to update current gridView_goods SelectedItem property and pass current datacontext as parameter, please refer to the following code.

    <TextBox  
        x:Name="txtModify"  
        Height="Auto"  
        AcceptsReturn="True"  
        ScrollViewer.VerticalScrollBarVisibility="Auto"  
        ScrollViewer.VerticalScrollMode="Auto"  
        Text="{Binding txtModify, Mode=TwoWay}"  
        TextChanged="txtModify_TextChanged"  
        TextWrapping="Wrap">  
        <Interactivity:Interaction.Behaviors>  
            <Core:EventTriggerBehavior EventName="TextChanged">  
                <Core:ChangePropertyAction  
                    PropertyName="SelectedItem"  
                    TargetObject="{Binding ElementName=gridView_goods}"  
                    Value="{Binding}" />  
            </Core:EventTriggerBehavior>  
        </Interactivity:Interaction.Behaviors>  
    </TextBox>  
    

    For multiple selectitem, you need to use InvokeCommandAction to call the command in the code behind and insert current datacontext into gridview selecteditems

    public MainPage()  
    {  
        this.InitializeComponent();  
        this.DataContext = this;  
    }  
    protected override void OnNavigatedTo(NavigationEventArgs e)  
    {  
        base.OnNavigatedTo(e);  
        var goods = new List<Good>() {  
        new Good(){TxtOriginal="text1",Uri="http-text1" },  
         new Good(){TxtOriginal="text2",Uri="http-text1" },  
          new Good(){TxtOriginal="text3",Uri="http-text1" },  
           new Good(){TxtOriginal="text4",Uri="http-text1" }  
        };  
        gridView_goods.ItemsSource = goods;  
    }  
    public ICommand GetSelectItem  
    {  
        get  
        {  
            return new CommadEventHandler<Good>((s) => {  
      
                gridView_goods.SelectedItems.Add(s);  
            });  
        }  
      
    }  
    

    Xaml

    <TextBox  
        x:Name="txtModify"  
        Height="Auto"  
        AcceptsReturn="True"  
        ScrollViewer.VerticalScrollBarVisibility="Auto"  
        ScrollViewer.VerticalScrollMode="Auto"  
        Text="{Binding txtModify, Mode=TwoWay}"  
        TextChanged="txtModify_TextChanged"  
        TextWrapping="Wrap">  
        <Interactivity:Interaction.Behaviors>  
            <Core:EventTriggerBehavior EventName="TextChanged">  
                <Core:InvokeCommandAction Command="{Binding DataContext.GetSelectItem, ElementName=gridView_goods}" CommandParameter="{Binding}" />  
            </Core:EventTriggerBehavior>  
        </Interactivity:Interaction.Behaviors>  
    </TextBox>  
      
      
    

    Thanks

    Nico Zhu

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.