locked
TextBox cotrol have event of "GotFocus" by itself when listview item is updated. RRS feed

  • Question

  • TextBox cotrol have event of "GotFocus" by itself when listview item is updated.

    I put both TextBox and ListView control in a Page.

    i added code to change ItemSource of ListView in the fuction of item click of event.

    when i click a item on listview, TextBox control have event of "GotFocus" by itselft.

    the TextBox control is not related to ListView. why this odd symptom is happen and is there any solution to avoid it?


    this is code.
    -----------------------------------------------------------------------------------------------------------
     protected override void OnNavigatedTo(NavigationEventArgs e)
     {
         UpdateListItem();
     }

     private void UpdateListItem()
     {
         string[] strItemList = new string[] { "sample value 1", "sample value2" };
         ListView.ItemsSource = strItemList;
     }

     private void ListViewItem_ContainerContentChanging(ListViewBase sender, ContainerContentChangingEventArgs args)
     {           
         TextBlock ViewItem = args.ItemContainer.ContentTemplateRoot as TextBlock;

         string strItem = (string)args.Item;

         ViewItem.Text = strItem;
     }

     private void ListView_ItemClick(object sender, ItemClickEventArgs e)
     {
         UpdateListItem();
     }

     private void TextBox_GotFocus(object sender, RoutedEventArgs e)
     {
         Debug.WriteLine("GotFocus event by itself");  
     }

    -----------------------------------------------------------------------------------------------------------


    this is xaml.

    -----------------------------------------------------------------------------------------------------------
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>

            <TextBox Grid.Row="0" FontSize="23" Text="This Control got focus" GotFocus="TextBox_GotFocus"/>
            <ListView  Name="ListView" Grid.Row="1" ContainerContentChanging="ListViewItem_ContainerContentChanging" IsItemClickEnabled="True" ItemClick="ListView_ItemClick">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <TextBlock FontSize ="25" Foreground="White"/>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
        </Grid>
    -----------------------------------------------------------------------------------------------------------

    this is screen




    • Edited by estmob Wednesday, December 3, 2014 7:18 AM
    Wednesday, December 3, 2014 7:17 AM

Answers

  • Because  reset ListView's DataSource will cause ListView lost focus, then page will move focus to next control which can be focused. But in this case, the page only has two controls of this kind.

    you can try this code, then you will know what I mean

    <Page
        x:Class="App2.MainPage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:App2"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="*"/>
                <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>
    
            <TextBox Grid.Row="0" FontSize="23" Text="This Control got focus" GotFocus="TextBox_GotFocus"/>
            <ListView  Name="ListView" Grid.Row="1" 
                       ContainerContentChanging="ListViewItem_ContainerContentChanging" 
                       IsItemClickEnabled="True" ItemClick="ListView_ItemClick" LostFocus="ListView_LostFocus" >
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <TextBlock FontSize ="25" Foreground="White"/>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
            <Button Content="Click" Grid.Row="2" GotFocus="Button_GotFocus"/>
        </Grid>
    
    </Page>

    namespace App2
    {
       
        public sealed partial class MainPage : Page
        {
            public MainPage()
            {
                this.InitializeComponent();
                this.NavigationCacheMode = NavigationCacheMode.Required;
            }
    
           
            protected override void OnNavigatedTo(NavigationEventArgs e)
            {
                UpdateListItem();
            }
    
            private void UpdateListItem()
            {
                string[] strItemList = new string[] { "sample value 1", "sample value2" };
                ListView.ItemsSource = strItemList;
            }
    
            private void ListViewItem_ContainerContentChanging(ListViewBase sender, ContainerContentChangingEventArgs args)
            {
                TextBlock ViewItem = args.ItemContainer.ContentTemplateRoot as TextBlock;
    
                string strItem = (string)args.Item;
    
                ViewItem.Text = strItem;
            }
    
            private void ListView_ItemClick(object sender, ItemClickEventArgs e)
            {
                UpdateListItem();
            }
    
            private void TextBox_GotFocus(object sender, RoutedEventArgs e)
            {
                Debug.WriteLine("GotFocus event by itself");
            }
    
            private void Button_GotFocus(object sender, RoutedEventArgs e)
            {
                Debug.WriteLine("GotFocus event by Button");
            }
    
            private void ListView_LostFocus(object sender, RoutedEventArgs e)
            {
                Debug.WriteLine("LostFocus event by ListView");
            }
    
        }
    }


    在現實生活中,你和誰在一起的確很重要,甚至能改變你的成長軌跡,決定你的人生成敗。 和什麼樣的人在一起,就會有什麼樣的人生。 和勤奮的人在一起,你不會懶惰; 和積極的人在一起,你不會消沈; 與智者同行,你會不同凡響; 與高人為伍,你能登上巔峰。



    Wednesday, December 3, 2014 8:47 AM