0

I am working in windows phone application where i have one ListBox control. And one button as inner control.

So in list 20 buttons after binding data. Now I want to change the data of button once it clicked.

This is my sample code. this is my first app windows phone.

 <ListBox Grid.Row="1" HorizontalAlignment="Left" Margin="10,10,0,0" Name="lstInstagramTags" VerticalAlignment="Top" Width="444" Height="562" SelectionChanged="lstInstagramTags_SelectionChanged">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal" Height="100" Margin="-10,-10,-10,-10">

                        <Button Click="Button_Click" Content="NeedToChangeThisValue" Width="150" FontSize="13" Height="60" Margin="-560,35,5,5" />
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

    private void Button_Click(object sender, RoutedEventArgs e)
            {
    HttpWebRequest myRequest = (HttpWebRequest)HttpWebRequest.Create(myUri);
                    myRequest.Method = "POST";
                    myRequest.ContentType = "application/x-www-form-urlencoded";
                    myRequest.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), myRequest);
    }

void GetRequestStreamCallback(IAsyncResult callbackResult)
        {
            HttpWebRequest myRequest = (HttpWebRequest)callbackResult.AsyncState;
            // End the stream request operation
            Stream postStream = myRequest.EndGetRequestStream(callbackResult);

            string PostData = "action=follow&access_token=966258514.201df4f.4b1c5015a7784a63aac00b9a902c4176";

            // Create the post data
            string postData = PostData;
            byte[] byteArray = Encoding.UTF8.GetBytes(postData);

            // Add the post data to the web request
            postStream.Write(byteArray, 0, byteArray.Length);
            postStream.Close();

            // Start the web request
            myRequest.BeginGetResponse(new AsyncCallback(GetResponsetStreamCallback), myRequest);
        }

void GetResponsetStreamCallback(IAsyncResult callbackResult)
        {
            HttpWebRequest request = (HttpWebRequest)callbackResult.AsyncState;
            HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(callbackResult);
            using (StreamReader httpWebStreamReader = new StreamReader(response.GetResponseStream()))
            {
                **string result** = httpWebStreamReader.ReadToEnd();

            }
        }

I want to change the button data based on the result (Marked Bold). I am not getting way to update any control on the basis of this result.

Please suggest me any good way to get this.

Thanks

Pankaj Mishra
  • 20,197
  • 16
  • 66
  • 103

2 Answers2

1

I think this question is mostly option based, so here are three ideas

  • Globul Button

Define a button in your current page

private Button helerButton;

   private void Button_Click(object sender, RoutedEventArgs e)
        {
            helerButton = (Button)sender;
            HttpWebRequest myRequest = (HttpWebRequest)HttpWebRequest.Create(myUri);
            myRequest.Method = "POST";
            myRequest.ContentType = "application/x-www-form-urlencoded";
            myRequest.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), myRequest);
        }

void GetResponsetStreamCallback(IAsyncResult callbackResult)
        {
            HttpWebRequest request = (HttpWebRequest)callbackResult.AsyncState;
            HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(callbackResult);
            using (StreamReader httpWebStreamReader = new StreamReader(response.GetResponseStream()))
            {
               string result = httpWebStreamReader.ReadToEnd();
               helerButton.Content = result

            }
        }
  • second approach

you can change the code architecture a little bit

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        HttpWebRequest myRequest = (HttpWebRequest)HttpWebRequest.Create(myUri);
        myRequest.Method = "POST";
        myRequest.ContentType = "application/x-www-form-urlencoded";
        myRequest.BeginGetRequestStream(r => 
        {
            var httpRequest = (HttpWebRequest)r.AsyncState;
            var httpResponse = (HttpWebResponse)httpRequest.EndGetResponse(r);
            Stream postStream = myRequest.EndGetRequestStream(r);
            string PostData = "action=follow&access_token=966258514.201df4f.4b1c5015a7784a63aac00b9a902c4176";           
            string postData = PostData;
            byte[] byteArray = Encoding.UTF8.GetBytes(postData);
            postStream.Write(byteArray, 0, byteArray.Length);
            postStream.Close();
            httpRequest.BeginGetResponse(q => 
            {
                    HttpWebRequest request = (HttpWebRequest)q.AsyncState;
                    HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(q);
                    using (StreamReader httpWebStreamReader = new StreamReader(response.GetResponseStream()))
                    {
                        string result = httpWebStreamReader.ReadToEnd();

                        Dispatcher.BeginInvoke(() =>
                        {
                            Button b = (Button)sender;
                            b.Content = result;
                            //for consideration 
                            //b.IsEnabled 

                        });
                    }
            }, httpRequest);

        }, myRequest);
    }
  • final approach( i, personally, will choose this)

Is to create Tasks Async and Await for Http Networking on Windows Phone

Swift Sharp
  • 2,604
  • 3
  • 30
  • 54
0

You need to use a visual tree helper for the same

Use this Method to dig inside the visual tree of a listbox

 public static T FindFirstElementInVisualTree<T>(DependencyObject parentElement) where T : DependencyObject
        {
            try
            {
                int childCount = VisualTreeHelper.GetChildrenCount(parentElement);
                if (childCount == 0)
                    return null;

                for (int i = 0; i < childCount; i++)
                {
                    var child = VisualTreeHelper.GetChild(parentElement, i);
                    if (child != null && child is T)
                    {
                        return (T)child;
                    }
                    else
                    {
                        var result = FindFirstElementInVisualTree<T>(child);
                        if (result != null)
                            return result;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            return null;
        }

And that's how you are going to use this method in code

ListBoxItem SelectedListBoxItem = this.lstInstagramTags.ItemContainerGenerator.ContainerFromIndex(int index) as ListBoxItem;
                if (SelectedListBoxItem == null)
                    return;
                // Iterate whole listbox tree and search for this items
                Button btn= common.FindFirstElementInVisualTree<Button>(SelectedListBoxItem );
                btn.Content="Hello";

And a Link too

Hope this helps.

Community
  • 1
  • 1
A.K.
  • 3,321
  • 1
  • 15
  • 27