Tulsi
Tulsi

Reputation: 151

How to get selected item value?

How to get ItemsControl display items selected Item textbox text value using MVVM pattern?

<ListBox Margin="0,25,0,0" Grid.Row="3" ItemsSource="{Binding Path=ViewModelSearchResults}" SelectedItem="{Binding Path=SelectedCategoryViewModel, Mode=TwoWay}">
                    <ItemsControl.ItemTemplate>
                        <DataTemplate >
                            <Grid>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="*"/>
                                    <ColumnDefinition/>
                                </Grid.ColumnDefinitions>
                                <TextBox Grid.Row="0" Grid.Column="0" Text="{Binding Path=CategoryName}" FontSize="14" FontWeight="Normal" />
                                <TextBox Grid.Row="0" Grid.Column="1" Text="{Binding Path=CategoryID}" FontSize="14" FontWeight="Normal" Visibility="Hidden" />
                            </Grid>
                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                </ListBox>

Upvotes: 0

Views: 520

Answers (2)

Snowbear
Snowbear

Reputation: 17274

Like getting anything else from Views: bind it! Yes, ItemsControl has SelectedItem but ItemsControl itself doesn't have selection behavior. You should use something like ListBox instead.

Upvotes: 1

Sreedharlal B Naick
Sreedharlal B Naick

Reputation: 156

You can bind SelectedItem to a property and access the required values through that property.

Upvotes: 1

Related Questions