Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WCF and WF
  4. Binding Array string to a textblock

Binding Array string to a textblock

Scheduled Pinned Locked Moved WCF and WF
wpfwcfdata-structuresquestion
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Aslesh
    wrote on last edited by
    #1

    Hi, Can we bind array of strings to a Textbloxk?

    B 1 Reply Last reply
    0
    • A Aslesh

      Hi, Can we bind array of strings to a Textbloxk?

      B Offline
      B Offline
      Brian Griggs
      wrote on last edited by
      #2

      Yes. Do you want the text to be just one item in the array, or all of them seperated by a comma or something?

      A 1 Reply Last reply
      0
      • B Brian Griggs

        Yes. Do you want the text to be just one item in the array, or all of them seperated by a comma or something?

        A Offline
        A Offline
        Aslesh
        wrote on last edited by
        #3

        If you have the answer for both of them that will be gr8 or else either of the one is fine

        B 1 Reply Last reply
        0
        • A Aslesh

          If you have the answer for both of them that will be gr8 or else either of the one is fine

          B Offline
          B Offline
          Brian Griggs
          wrote on last edited by
          #4

          For a single item you can specify the index in the binding (StrArray would be the string array in your data class):

          Text="{Binding Path=StrArray[0]}"

          The trick to get all the values in a string seperated by commas is to use a value converter - something like this:

          public class StrArrayConverter : IValueConverter
          {
          public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
          {
          string[] strArr = value as string[];
          if (strArr != null)
          {
          StringBuilder sb = new StringBuilder();
          foreach (string s in strArr)
          {
          sb.Append(s);
          sb.Append(", ");
          }
          return sb.ToString();
          }

              return null;
          }
          public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
          {
              return null;
          }
          

          }

          <TextBlock>
          <TextBlock.Text>
          <Binding Path="StrArr">
          <Binding.Converter>
          <l:StrArrayConverter/>
          </Binding.Converter>
          </Binding>
          </TextBlock.Text>
          </TextBlock>

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups