0

Hi i have a requirement i need your help for it:-

I have a set of phone numbers of type string separated by a comma , now i want to assign each phone number to hyperlink and on click of it will invoke the PhoneCallTask and make a call to that particular phone number.

1) So , how to assign each phone number to a hyperlink(should we dynamically generate the hyperlink? in c# codebehind)

2)if so , how to dynamically generate hyperlink buttons and add it to a stack panel present in a listbox ?

3)How would i know which Hyperlinkbutton is clicked?

4)All HyperlinkButton's would point to same hyperlink click event?

Thanks in Advance.

1 Answer 1

3

In your MainPage.xaml, add this inside the ContentPanel control:

<ListBox x:Name="PhoneNumbersList">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <HyperlinkButton Content="{Binding}" Click="PhoneNumberHyperlinkButton_Click" />
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

Then, on the MainPage code behind, add this code:

public MainPage()
{
    InitializeComponent();

    var phoneNumbers = new string[] { "9999999", "8888888", "7777777" };

    PhoneNumbersList.ItemsSource = phoneNumbers;
}

private void PhoneNumberHyperlinkButton_Click(object sender, RoutedEventArgs e)
{
    var phoneNumberHyperlinkButton = (HyperlinkButton)sender;

    var phoneNumber = (string)phoneNumberHyperlinkButton.Content;

    new Microsoft.Phone.Tasks.PhoneCallTask()
    {
        PhoneNumber = phoneNumber
    }.Show();
}

That's it!

Sign up to request clarification or add additional context in comments.

6 Comments

first of all , Thanks a lot for the Reply you made it so simple. once again thanks, in case of any queries i will contact you again.
What should i do if i want to bind it to a particluar stackpanel inside a listbox specfically? i tried it in this way :::- <StackPanel Orientation="Horizontal"> <HyperlinkButton FontFamily="Calibri" VerticalAlignment="Center" HorizontalAlignment="Left" Click="HyperlinkButton_Click" Content="{Binding PhoneNumbers}" Foreground="Blue" Width="300" FontSize="26"></HyperlinkButton> </StackPanel> but it doesnot work for me and it is not being displayed ?
i want to know how to bind it to you particular stackpanel inside a listbox instead of binding directly to a listbox ?
Don't know what you intend to do: if it's a list of items (like an array or List<>), you should bind it to a control that handles lists, not a control that handles one item, like a StackPanel... what is the purpose you are trying to achieve?
I have a parent listbox, inside the datatemplate of which I have multiple stackpanels. Inside one of the stack panels I need to show this list of phone numbers one below the other. So it means I have to nest one list box inside the other and so, I can't access the nested listbox in code behind directly. Also the binding is not working as you mentioned above whenever I nest the listboxes.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.