Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6805129
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:32:57+00:00 2026-05-26T19:32:57+00:00

The following xaml code produces a ListView with three columns. The ListView ItemsSource is

  • 0

The following xaml code produces a ListView with three columns. The ListView ItemsSource is an observablecollection. The first column shows the name of the object in a particular row. The second and third columns show buttons associated to the object in a particular row.

<Grid Width="497" Height="260">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="309*" />
        <ColumnDefinition Width="188*" />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="478*" />
        <RowDefinition Height="4*" />
    </Grid.RowDefinitions>
    <GroupBox Header="ObservableCollection Object List" HorizontalAlignment="Left" Width="497" BorderBrush="Black" BorderThickness="2" Grid.ColumnSpan="2">
        <ListView ItemsSource="{Binding Path=CollectionList}" Name="QueueListView">
            <ListView.Resources>
                <Style TargetType="{x:Type ListViewItem}">
                    <EventSetter Event="PreviewGotKeyboardFocus" Handler="SelectCurrentItem"/>
                </Style>
            </ListView.Resources>
            <ListView.SelectedItem>
                <Binding Path="SelectedObjectFromList" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged" >
                </Binding>               
            </ListView.SelectedItem>
            <ListView.View>
                <GridView>
                    <GridViewColumn Width="140" Header="Object Name" DisplayMemberBinding="{Binding Name}" />
                    <GridViewColumn Width="160" Header="Property Information">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <Button Content="Get Property Info" Command="{Binding Path=GetObjProperties}"
                                        DataContext="{Binding DataContext, RelativeSource={RelativeSource FindAncestor, AncestorType=ListView}}" />                      
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                    <GridViewColumn Width="180" Header="Transfer">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <Button Content="Transfer Object" Command="{Binding Path=TransferObjHere}"
                                        DataContext="{Binding DataContext, RelativeSource={RelativeSource FindAncestor, AncestorType=ListView}}" />
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                </GridView>
            </ListView.View>
        </ListView>
    </GroupBox>
</Grid>

The SelectCurrentItem event handler binds the ListView.SelectedItem to the SelectedObjectFromList property in my view model when the user clicks the “Transfer Object” or “Get Property Info”button. I an use this property to expose the selected object to my view model.

Here is my SelectCurrentItem handler c# code in code behind:

    protected void SelectCurrentItem(Object sender, KeyboardFocusChangedEventArgs e)
    {
        ListViewItem viewItem = (ListViewItem)sender;
        viewItem.IsSelected = true;
    }

This works great! When the user has clicked a button, the SelectedObjectFromList property is updated from the ListView observablecollection object for that row. (No need to manually click the ListView row to set the property before clicking the button.)

One problem: As I click buttons in the list, the recently selected rows still appear to be selected (they are highlighted in the GUI).

I tried to solve the problem by setting the isFocused property of the ListView:

    protected void SelectCurrentItem(Object sender, KeyboardFocusChangedEventArgs e)
    {
        ListViewItem Item = (ListViewItem)sender;
        Item.IsSelected = true;
        Item.IsFocused = true;
    }

Of course that gives me a StackOverflow exeption :). Does anyone have example code to update the ListView.Selection in the GUI in this case? Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T19:32:58+00:00Added an answer on May 26, 2026 at 7:32 pm

    ListViews allow you to have multiple items selected at once. When you click on a 2nd item, you’re not resetting the IsSelected property of the old selected item to False, so it is staying selected

    You can try either setting the ListView’s SelectionMode to Single

    <ListView SelectionMode="Single" ... />
    

    or try getting the old SelectedItem and unselecting it when you’re selecting the new one

    protected void SelectCurrentItem(Object sender, KeyboardFocusChangedEventArgs e)
    {
        var temp = myListBox.SelectedItem as ListViewItem;
        if (temp != null)
            temp.IsSelected = false;
    
        ListViewItem viewItem = (ListViewItem)sender;
        viewItem.IsSelected = true;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a following XAML: <ComboBox Name=groupComboBox ItemsSource={Binding Path=MyServiceMap.Groups} DisplayMemberPath={Binding Name}/> In the code
I have the following XAML Code: <sdk:DataGrid Margin=58,8,52,18 Name=dataGridTickets> <sdk:DataGrid.Columns> <sdk:DataGridTextColumn x:Name=ticketNoColumn Header=Ticket No.
Hi I have following XAML code which is the output from XamlWriter.Save(): <StackPanel Name=itemStack
I got the following xaml code (simplified) : <StackPanel Grid.Row=2> <TreeView x:Name=a VerticalAlignment=Stretch BorderThickness=0>
I am binding a listbox with contacts address using following xaml code <ListBox Name=ContactResultsDataLINQ
I have the following xaml-Code: <Border x:Name=border> <Border.Style> <Style TargetType={x:Type Border}> <Style.Triggers> <DataTrigger Binding={Binding
I have the following XAML code: <ListBox ItemsSource={Binding Languages}> <ListBox.ItemTemplate> <DataTemplate> <StackPanel> <Image Source={Binding
I have created two buttons using the following XAML code. <Button x:Name=Button1 Width=100 Content=Button1
I need to convert this following XAML into code-behind: <ComboBox SelectedItem={Binding Level} ItemsSource={Binding Levels}
I'm developing a Windows Phone app. I have the following XAML code: <Grid x:Name=ContentPanel

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.