×

Discussion Board

Results 1 to 10 of 10
  1. #1
    Regular Contributor
    Join Date
    Nov 2006
    Location
    Switzerland
    Posts
    476

    Unable to get text from text block

    Hi,

    I've an app with a Pivot and longlist selector. The XAML code snippet looks as following:
    Code:
            <phone:Pivot Title="App Title" Grid.ColumnSpan="2">
                <!--Pivot item one-->
                <phone:PivotItem Header="Title Pivot Item One">
                    <!--Double line list with text wrapping-->
                    <phone:LongListSelector x:Name="LLS_1" Margin="0,0,-12,0" ItemsSource="{Binding Items_1}">
                        <phone:LongListSelector.ItemTemplate>
                            <DataTemplate>
                                <StackPanel Margin="0,0,0,17">
                                    <TextBlock x:Name="TextLineOne" Text="{Binding LineOne}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
                                    <TextBlock x:Name="TextLineTwo" Text="{Binding LineTwo}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextSubtleStyle}"/>
                                </StackPanel>
                            </DataTemplate>
                        </phone:LongListSelector.ItemTemplate>
                    </phone:LongListSelector>
                </phone:PivotItem>
    Now, I'd like to get the text of all items in this longlist (text block names: TextLineOne and & TextLineTwo), when someone clicks in the app bar:
    Code:
            private void ApplicationBarExportItems_Click(object sender, EventArgs e)
            {
    
            }
    But how can I do this? I couldn't find a code example on the Internet. Can someone help me?

    Thank you very much!

  2. #2
    Nokia Developer Champion
    Join Date
    Feb 2013
    Location
    Dublin, Ireland
    Posts
    552

    Re: Unable to get text from text block

    Use the LongListSelector_SelectionChanged event, this sample should help...

    http://code.msdn.microsoft.com/wpapp...-item-30ced444

    (You can also bind to SelectedItem if you're using the MVVM approach)

  3. #3
    Regular Contributor
    Join Date
    Nov 2006
    Location
    Switzerland
    Posts
    476

    Re: Unable to get text from text block

    I don't want to use the "SelectionChanged" event, because the user should click on an item in the app bar to get all text of the longlist.

    I've tried to write the contents of the longlist selector to an array by using the CopyTo-function:
    Code:
    private void ApplicationBarExportItems_Click(object sender, EventArgs e)
    {
    string[] data = new string[LLS_1.ItemsSource.Count];
    LLS_1.ItemsSource.CopyTo(data, 0);
    
    StringBuilder sb = new StringBuilder();
    for (int index = 0; index < LLS_1.ItemsSource.Count; index++)
         sb.AppendLine(string.Join(";", data[index]));
    
    MessageBox.Show(sb.ToString());
    }
    Anyway, the app crashes after the line with the CopyTo function...

    BTW: I'm already using the MVVM approach.

  4. #4
    Nokia Developer Champion
    Join Date
    Feb 2013
    Location
    Dublin, Ireland
    Posts
    552

    Re: Unable to get text from text block

    Ah I see, so when a user taps on the button (from the AppBar), you want to loop through all the items in the LongListSelector?

    Also, in Debug mode, what exception is thrown and where?

  5. #5
    Regular Contributor
    Join Date
    Nov 2006
    Location
    Switzerland
    Posts
    476

    Re: Unable to get text from text block

    Yes, that's exactly what I'd like to do.

    The debugger says at the line with the CopyTo-function: LLS_1 - The name 'LLS_1' does not exist in the current context
    Last edited by patrickfrei; 2014-01-05 at 17:54.

  6. #6
    Regular Contributor
    Join Date
    Nov 2006
    Location
    Switzerland
    Posts
    476

    Re: Unable to get text from text block

    Ahh, just found the issue in the "locals" of the debugger:
    Message -- "Target array type is not compatible with the type of items in the collection."

    That's also strange as both items from the text blocks (x:Name="TextLineOne" & x:Name="TextLineTwo") are defined as String.
    Last edited by patrickfrei; 2014-01-05 at 17:54.

  7. #7
    Nokia Developer Champion
    Join Date
    Feb 2013
    Location
    Dublin, Ireland
    Posts
    552

    Re: Unable to get text from text block

    Quote Originally Posted by patrickfrei View Post
    That's also strange as both items from the text blocks (x:Name="TextLineOne" & x:Name="TextLineTwo") are defined as String.
    TextBlocks are only used in an ItemTemplate of a Control to display the data bound to them, the data itself (from Control.ItemSource) would not be a TextBlock(s). The ItemSource contains the list of data which was supplied to the control in the first place. Does that make sense?

    (Also, not to confuse things but you're not using MVVM when you do things this way)

  8. #8
    Regular Contributor
    Join Date
    Nov 2006
    Location
    Switzerland
    Posts
    476

    Re: Unable to get text from text block

    Here is a code snippet from what I've done in MainViewModel.cs:
    Code:
    public class MainViewModel : INotifyPropertyChanged
    {
            public MainViewModel()
            {
                this.Items_1 = new ObservableCollection<ItemViewModel>();
            }
    
            public ObservableCollection<ItemViewModel> Items_1 { get; private set;}
           
            public void LoadData_1()
            {
                    this.Items_1.Add(new ItemViewModel() { LineOne = "This is line one / first entry", LineTwo = "This is line two / first entry" });
                    this.Items_1.Add(new ItemViewModel() { LineOne = "This is line one / second entry", LineTwo = "This is line two / second entry" });
            }
    Everything works fine, i.e. all items in the longlist are properly filled. But I'm unable to put them all together in one string (kind of export function), i.e. the string should contain...
    This is line one / first entry" + "This is line two / first entry" + "This is line one / second entry" + "This is line two / second entry"
    Last edited by patrickfrei; 2014-01-05 at 18:30.

  9. #9
    Nokia Developer Champion
    Join Date
    Feb 2013
    Location
    Dublin, Ireland
    Posts
    552

    Re: Unable to get text from text block

    If the data you want to display is already in ViewModel (MainViewModel.Items_1), you don't need to get it from the LongListSelector. What I would recommend is...

    1. A button on the AppBar should use a Command (calls a method via binding)
    2. The Command calls a method on your ViewModel which displays the string

    The following will help you use Commands with the AppBar... http://bindableapplicationb.codeplex.com/

  10. #10
    Regular Contributor
    Join Date
    Nov 2006
    Location
    Switzerland
    Posts
    476

    Re: Unable to get text from text block

    Now, I got it. I had to set...
    Code:
    private void LLS_1_SelectionChanged(object sender, SelectionChangedEventArgs e)
            {
                ViewModels.ItemViewModel lbi = (sender as LongListSelector).SelectedItem as ViewModels.ItemViewModel;
                MessageBox.Show(lbi.LineOne.ToString() + "\n" +lbi.LineTwo.ToString());
            }
    ...in order to get the data from both text blocks.

Similar Threads

  1. How to creat Custom colored text in text block ?
    By sskiki in forum Windows Phone General
    Replies: 3
    Last Post: 2013-11-27, 08:06
  2. How to creat Custom colored text in text block ?
    By Ahmed Emad in forum Windows Phone General
    Replies: 7
    Last Post: 2013-04-15, 09:28
  3. unable to send text message
    By mind_freak in forum [Archived] Qt Mobility Project
    Replies: 6
    Last Post: 2011-10-25, 05:06
  4. Unable to send text SMS
    By deepujain in forum Mobile Java Networking & Messaging & Security
    Replies: 2
    Last Post: 2010-05-03, 07:28
  5. unable to get text from username and password editor
    By s_mehdi76 in forum Symbian User Interface
    Replies: 7
    Last Post: 2008-04-04, 08:34

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×