locked
Trigger listview click event

    Question

  • Dear team,

                    i have a bind data in the list view. i want to fire the first value clicked event. 

    public class Customer
            {
                public String Topic { get; set; }
                public String Slno { get; set; }
                public String SelectedAnswer { get; set; }
                public String Answer { get; set; }
                public BitmapImage Image { get; set; }
    
            }
     
    private void itemclicked(object sender, ItemClickEventArgs e)
            {
    
                clearTic();           
                Customer items =(Customer)e.ClickedItem;
                slno = items.Slno;
    
                popupQuestionDisplay();
                
            }
    
            void popupQuestionDisplay()
            {
                var custs = from c in cs.xe1.Elements("Table") where (c.Element("Qslno").Value == slno) select c;
                foreach (var item in custs)
                {
    
                    
                    rtbQuestion.Blocks.Clear();
                    answer_a.Blocks.Clear();
                    answer_b.Blocks.Clear();
                    answer_c.Blocks.Clear();
                    answer_d.Blocks.Clear();
                    answer_e.Blocks.Clear();
                    clearTic();
                    tbQuestionNumber.Text = "Question No : " + item.Element(System.Xml.Linq.XName.Get("Qslno")).Value;
                    rtbQuestion.Blocks.Add(questiondisplay(replaceString(item.Element(System.Xml.Linq.XName.Get("Question")).Value.ToString())));
                    answer_a.Blocks.Add(questiondisplay(replaceString(item.Element(System.Xml.Linq.XName.Get("optA")).Value.ToString())));
                    answer_b.Blocks.Add(questiondisplay(replaceString(item.Element(System.Xml.Linq.XName.Get("optB")).Value.ToString())));
                    answer_c.Blocks.Add(questiondisplay(replaceString(item.Element(System.Xml.Linq.XName.Get("optC")).Value.ToString())));
                    answer_d.Blocks.Add(questiondisplay(replaceString(item.Element(System.Xml.Linq.XName.Get("optD")).Value.ToString())));
                    answer_e.Blocks.Add(questiondisplay(replaceString(item.Element(System.Xml.Linq.XName.Get("optE")).Value.ToString())));
                    Image im = (Image)FindName("Tic" + item.Element(System.Xml.Linq.XName.Get("Answer")).Value.ToString());
                    im.Visibility=Visibility.Visible;
                    
                }
    
            }
    kindly help me
    Saturday, November 8, 2014 7:05 AM

Answers

  • You could use a ListViewItemAutomationPeer and an IInvokeProvider to programmatically click a ListViewItem once the ListView has been loaded:

    public MainPage()
            {
                this.InitializeComponent();
    
                lv.ItemsSource = new List<MyItem> { new MyItem { Name = "1"}, new MyItem { Name = "2" } };
    
                this.Loaded += MainPage_Loaded;
            }
    
            void MainPage_Loaded(object sender, RoutedEventArgs e)
            {
                ListViewItem lvi = lv.ContainerFromIndex(0) as ListViewItem; //first item (index = 0)
    
                ListViewItemAutomationPeer peer = new ListViewItemAutomationPeer(lvi);
    
                IInvokeProvider invokeProv = peer.GetPattern(PatternInterface.Invoke) as IInvokeProvider;
                invokeProv.Invoke();
            }
    

    <ListView x:Name="lv" ItemClick="lv_ItemClick" IsItemClickEnabled="True">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <StackPanel>
                            <TextBlock Text="{Binding Name}"/>
                        </StackPanel>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
    
    Please remember to mark helpful posts as answer and/or helpful.
    • Marked as answer by mann madhan1 Saturday, November 8, 2014 10:47 AM
    Saturday, November 8, 2014 10:40 AM
  • Thanks magnus,

          i used the following code and it is also working 

     EmployeeRecord.SelectedIndex = 0;
                Customer items = (Customer)EmployeeRecord.SelectedItem;
                slno = items.Slno;
                popupQuestionDisplay();
                EmployeeRecord.SelectedIndex = -1;
    Thanks a lot.
    • Marked as answer by mann madhan1 Saturday, November 8, 2014 10:49 AM
    Saturday, November 8, 2014 10:48 AM

All replies

  • why want to fake mouse click? cant you do this after setting thee itemssource?

    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    • Proposed as answer by Dave SmitsMVP Saturday, November 8, 2014 7:13 AM
    • Unproposed as answer by Dave SmitsMVP Saturday, November 8, 2014 7:13 AM
    Saturday, November 8, 2014 7:13 AM
  • Thanks Dave,

                      any sample code please

    Saturday, November 8, 2014 7:47 AM
  • You could use a ListViewItemAutomationPeer and an IInvokeProvider to programmatically click a ListViewItem once the ListView has been loaded:

    public MainPage()
            {
                this.InitializeComponent();
    
                lv.ItemsSource = new List<MyItem> { new MyItem { Name = "1"}, new MyItem { Name = "2" } };
    
                this.Loaded += MainPage_Loaded;
            }
    
            void MainPage_Loaded(object sender, RoutedEventArgs e)
            {
                ListViewItem lvi = lv.ContainerFromIndex(0) as ListViewItem; //first item (index = 0)
    
                ListViewItemAutomationPeer peer = new ListViewItemAutomationPeer(lvi);
    
                IInvokeProvider invokeProv = peer.GetPattern(PatternInterface.Invoke) as IInvokeProvider;
                invokeProv.Invoke();
            }
    

    <ListView x:Name="lv" ItemClick="lv_ItemClick" IsItemClickEnabled="True">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <StackPanel>
                            <TextBlock Text="{Binding Name}"/>
                        </StackPanel>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
    
    Please remember to mark helpful posts as answer and/or helpful.
    • Marked as answer by mann madhan1 Saturday, November 8, 2014 10:47 AM
    Saturday, November 8, 2014 10:40 AM
  • Thanks magnus,

          i used the following code and it is also working 

     EmployeeRecord.SelectedIndex = 0;
                Customer items = (Customer)EmployeeRecord.SelectedItem;
                slno = items.Slno;
                popupQuestionDisplay();
                EmployeeRecord.SelectedIndex = -1;
    Thanks a lot.
    • Marked as answer by mann madhan1 Saturday, November 8, 2014 10:49 AM
    Saturday, November 8, 2014 10:48 AM