Powered by MSDN

US - English
NEW! Silverlight 5 is available Learn More

Recover text value (Silverlight application) RSS

8 replies

Last post Oct 14, 2008 08:10 AM by Frank87

(0)
  • Frank87

    Frank87

    Member

    5 Points

    59 Posts

    Recover text value (Silverlight application)

    Oct 08, 2008 03:35 PM | LINK

     Hi, I have a silverlight application, and I need to recover the values of several textboxes within a listbox. Theese textboxes are created in the Page.xaml like the following:

    <ListBox Margin="23,8,23,23" ItemsSource="{Binding '', Mode=OneWay, Path=ProgrammazioneCompleta, Source={StaticResource ProgrammazioneDS}}" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Grid.RowSpan="3" Grid.Column="1" Grid.Row="1" x:Name="ListBox1">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal" Margin="2">
                            <TextBlock Text="{Binding Ora}"/>
                            <TextBlock Text=" - "/>
                            <TextBox Text="{Binding Nome}" BorderBrush="Transparent" BorderThickness="0"/>
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>

    In my Page.xaml.cs I can see the listbox, but not the textboxes .How can I recover the text value of the textboxes ??

    Thanks for the help ;)

  • HarshBardhan

    HarshBardhan

    Star

    10118 Points

    1749 Posts

    Re: Recover text value (Silverlight application)

    Oct 08, 2008 03:40 PM | LINK

    Hi,

    If your Binding mode is two way you can get that in your DataContext.

    Mark as answer if this post answered your question.

    Harsh Bardhan
  • sladapter

    sladapter

    All-Star

    43609 Points

    7910 Posts

    Re: Recover text value (Silverlight application)

    Oct 08, 2008 03:45 PM | LINK

    You are using data binding.  If you set TwoWay binding for the TextBox,  the value user entered in the box should be automatically update the DataObject that bind to that row.

      <TextBox Text="{Binding Nome, Mode=Two Way}" BorderBrush="Transparent" BorderThickness="0"/>

    If you check the Nome field on you DataObject in the ProgrammazioneDS, you should be able to see the change. You do not need to access the UI element to retrieve that data.

     

    Sally Xu
    Software Engineer
    Aprimo, Inc

    Please remember to mark the replies as answers if they answered your question
  • Frank87

    Frank87

    Member

    5 Points

    59 Posts

    Re: Recover text value (Silverlight application)

    Oct 13, 2008 08:44 AM | LINK

     I've setted the Binding mode in TwoWay, but I don't know how to access the ProgrammazioneDS to check the value of Nome. My DataObject is an ObservableCollection<Palinsesto>, and it is used in a class added to the project (Palinsesto.cs). I need to recover the text values in my Page.xaml.cs.

    I've also tried to use the DataContext, but its value is null... [:(]

  • HarshBardhan

    HarshBardhan

    Star

    10118 Points

    1749 Posts

    Re: Recover text value (Silverlight application)

    Oct 13, 2008 11:09 AM | LINK

    Hi,

    you can try with something like this.

    I did that on mouse down handler.

    Person p = (Person)this.DataContext;

    //Here Person is my Object with which i bounded  my userControl

     

    Mark as answer if this post answered your question.

    Harsh Bardhan
  • Frank87

    Frank87

    Member

    5 Points

    59 Posts

    Re: Recover text value (Silverlight application)

    Oct 13, 2008 04:03 PM | LINK

     I've tried to convert the DataContext, but it still remains null...

     I try to exlpain what happens:

     _ I read from an xml file, and write its content in a series of textblocks and textboxes, within a listbox:

    (this is Palinsesto.cs file) 

                    XmlReaderSettings settings = new XmlReaderSettings();
    settings.ConformanceLevel = ConformanceLevel.Fragment;
    settings.IgnoreWhitespace = true;
    XmlReader reader = XmlReader.Create("Programmazione.xml", settings);
    ObservableCollection ListaProgrammazione = new ObservableCollection();
    while (!reader.EOF)
    {
    reader.Read();
    if (reader.Name == "testo")
    {
    linea = reader.ReadElementContentAsString();
    trattino = linea.IndexOf(" - ");
    orario = linea.Substring(0, trattino);
    canale = linea.Substring(trattino + 3, linea.Length - trattino - 3);
    ListaProgrammazione.Add(new Palinsesto(orario, canale));
    }
    }
    return ListaProgrammazione; //this is the object used for binding
      

    _ Once I have the listbox filled, I have to write the content of the texboxes in the xml file, on the click event button; 

    (this is Page.xaml.cs file)

            void Click(object sender, RoutedEventArgs e)
    {
                object[] array = new object[24];
    object valori = ListBox1.Items;
    int i = 0;
    for (i = 0; i < ListBox1.Items.Count; i++ )
    {
    ListBox1.SelectedIndex = i;
    ListBox1.Items.CopyTo(array, 0);
    //Palinsesto valore = (Palinsesto)ListBox1.DataContext;
    Palinsesto pippo = (Palinsesto)array[i];
    }
    }
     
    If I run my application I can see that the object array contains the items of the listbox, but they 
    aren't accessible because they are in this format: SilverlightApplication1.Palinsesto:
      
     If I explore the items, I can see that they contains the values that I need to recover:
     
    The problem is that I don't kwon how to access theese values... 
     
  • neal.gabriel

    neal.gabriel

    Contributor

    4520 Points

    789 Posts

    Re: Re: Recover text value (Silverlight application)

    Oct 13, 2008 07:09 PM | LINK

    Hi,

    If you need to get the values from a list, you need to create an object for that and loop it.

    Eg:

    Here is your Class Object which has all the data

    Palinsesto pippo = (Palinsesto)array

    What you have to do is as follows

    foreach(var oPippo in pippo)

    {

    strNome = oPippo.Nome;

    }

     

    HTH

    Neal Gabriel

    Regards...

    Neal Gabriel
    Follow me on twitter
    My Blog
  • HarshBardhan

    HarshBardhan

    Star

    10118 Points

    1749 Posts

    Re: Recover text value (Silverlight application)

    Oct 14, 2008 07:09 AM | LINK

    Hi,

    You are almost there.

    You have to cast that to appropriate object and loop throgh it.

    Mark as answer if this post answered your question.

    Harsh Bardhan
  • Frank87

    Frank87

    Member

    5 Points

    59 Posts

    Re: Recover text value (Silverlight application)

    Oct 14, 2008 08:10 AM | LINK

     Now it works !! ..Thank you very much guys !! [:D]