0
votes

I have set the DataTemplate for the Listbox item such that it contains an TextBlock and an TextBox.

The problem I am facing is, when the list is displayed its selectionindex is -1, and suppose if the user taps directly into the textbox of any of the listitem then the that Listitem hasn't been set an the selecteditem of the listbox.

How can I change/set the selectedindex/item of the listbox when the user focus directly to the textbox?


Ok let me be more clear

my ListBox DataTemplate is as follows

<ListBox.ItemTemplate>
   <DataTemplate>
     <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="325"/>
            <ColumnDefinition Width="80"/>
        </Grid.ColumnDefinitions>

       <TextBlock  Text="{Binding ProdName}" Margin="0,5,0,0" Width="315" FontSize="36" Grid.Column="0"  ></TextBlock>
       <TextBox InputScope="Number" Grid.Column="1" Text="{Binding ProdQty}"  TextAlignment="Center" Width="80" HorizontalAlignment="Left" Name="txtQty" GotFocus="ClearZero" LostFocus="TextBoxLostFocus"></TextBox>

      </Grid>
  </DataTemplate>

Now when the page is opened the listbox if populated and its SelectedIndex is -1. Now suppose if the user taps(or sets focus) directly into the say 3rd TextBox (i.e the textbox of the third ListItem in the listbox), i want the listbox SelectedIndex to be changed from -1 to 2 (3rd items index).

When i am retriving the SelectedIndex of the ListBox when the 3rd(say) TextBox receives focus, its returning an value of -1.

Hope i am clear. Thanks

1

1 Answers

0
votes

Tapping on it sets SelectedItem and SelectObject, so it's perfectly reasonable for them to be -1 and null initially.

If you want to initialise it then just set selectedindex to one of the items in the list, after it's populated. NB check to see if items.count > 0 before you do that.