locked
Listview lose the focus RRS feed

  • Question

  • First i select an item in my listview. - afterwards when i click for example a button in the same window, the selected item in the listview lose the focus.

    How can i reach that the listview have always the focus on the selected item?!? (i think in winforms there was a property called "hideselection")

    Thursday, July 26, 2007 1:44 PM

Answers

  • Hi,

    That the ListView loses focus when you click the button, is normal. After clicking the Button, the Button has focus. But you can do the following to give the ListView focus after clicking the button.

    Give a name to Window, e.g. window, then define that the Window is  a focus scope. Then after clicking the buton, you set focus back on the listview in the click event of the button.

    Code Snippet

    <Window
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        x:Class="Sample.Window1"
        Height="300" Width="300"
        x:Name="window"
                FocusManager.IsFocusScope="True"
    >
      <Grid>
        <StackPanel>
        <ListView x:Name="listview" Canvas.Left="10" Canvas.Top="10">
          <ListViewItem>
            TEST
          </ListViewItem>
          <ListViewItem>
            TEST
          </ListViewItem>
          <ListViewItem>
            TEST
          </ListViewItem>
          <ListViewItem>
            TEST
          </ListViewItem>
          <ListViewItem>
            TEST
          </ListViewItem>
        </ListView>
        <Button Content="Give focus back now!" Click="btn_Click"></Button>
        </StackPanel>
      </Grid>
    </Window>



    Code Snippet

    using System;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Input;

    namespace Sample
    {
      ///
      /// Interaction logic for Window1.xaml
      ///

        public partial class Window1 : System.Windows.Window
        {
            public Window1()
            {
                InitializeComponent();
            }

            private void btn_Click(object sender, RoutedEventArgs e)
            {
                FocusManager.SetFocusedElement(window,(IInputElement)listview);
               
            }
        }
    }


    Best regards,

    Benny
    Thursday, July 26, 2007 2:01 PM

All replies

  • Hi,

    That the ListView loses focus when you click the button, is normal. After clicking the Button, the Button has focus. But you can do the following to give the ListView focus after clicking the button.

    Give a name to Window, e.g. window, then define that the Window is  a focus scope. Then after clicking the buton, you set focus back on the listview in the click event of the button.

    Code Snippet

    <Window
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        x:Class="Sample.Window1"
        Height="300" Width="300"
        x:Name="window"
                FocusManager.IsFocusScope="True"
    >
      <Grid>
        <StackPanel>
        <ListView x:Name="listview" Canvas.Left="10" Canvas.Top="10">
          <ListViewItem>
            TEST
          </ListViewItem>
          <ListViewItem>
            TEST
          </ListViewItem>
          <ListViewItem>
            TEST
          </ListViewItem>
          <ListViewItem>
            TEST
          </ListViewItem>
          <ListViewItem>
            TEST
          </ListViewItem>
        </ListView>
        <Button Content="Give focus back now!" Click="btn_Click"></Button>
        </StackPanel>
      </Grid>
    </Window>



    Code Snippet

    using System;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Input;

    namespace Sample
    {
      ///
      /// Interaction logic for Window1.xaml
      ///

        public partial class Window1 : System.Windows.Window
        {
            public Window1()
            {
                InitializeComponent();
            }

            private void btn_Click(object sender, RoutedEventArgs e)
            {
                FocusManager.SetFocusedElement(window,(IInputElement)listview);
               
            }
        }
    }


    Best regards,

    Benny
    Thursday, July 26, 2007 2:01 PM
  • Hi,

    Or ... (I've got to wake up).

    listview.Focus() in the Click event. That's all. Much simpler.

    Best regards,

    Benny
    Thursday, July 26, 2007 2:07 PM