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. WPF
  4. DataTemplate with List binding

DataTemplate with List binding

Scheduled Pinned Locked Moved WPF
wpfwcfhelptutorialquestion
8 Posts 3 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.
  • P Offline
    P Offline
    pbalaga
    wrote on last edited by
    #1

    Hi, I managed to bring to work simple data template in a ListView control:

    <DataTemplate>
    <TextBox Text="{Binding Path=boo}" />
    </DataTemplate>

    Class of the items added to the ListView contains property 'boo' of type string. But how to do similar binding for a custom complex type. Let's say instead of string boo, I have List<int> integers;. What I'm trying to achieve is to make the textbox display the numbers in the list separated with commas (e.g. 3,5,15,2). In other words, is it possible to do something like

    <TextBox Text="{Binding Path=integers}" />

    by providing how the target should be formatted? The main issue is that it should be a two-way binding (with ability to convert "1,5,4" back to List). What direction to go? I assume I could implement a string property, i.e. 'NumbersString' returning joined contents of the list in form of a single string. Or maybe it has something to do with converters? I only hope my explanation is clear enough. Thanks in advance, Paul

    P A 2 Replies Last reply
    0
    • P pbalaga

      Hi, I managed to bring to work simple data template in a ListView control:

      <DataTemplate>
      <TextBox Text="{Binding Path=boo}" />
      </DataTemplate>

      Class of the items added to the ListView contains property 'boo' of type string. But how to do similar binding for a custom complex type. Let's say instead of string boo, I have List<int> integers;. What I'm trying to achieve is to make the textbox display the numbers in the list separated with commas (e.g. 3,5,15,2). In other words, is it possible to do something like

      <TextBox Text="{Binding Path=integers}" />

      by providing how the target should be formatted? The main issue is that it should be a two-way binding (with ability to convert "1,5,4" back to List). What direction to go? I assume I could implement a string property, i.e. 'NumbersString' returning joined contents of the list in form of a single string. Or maybe it has something to do with converters? I only hope my explanation is clear enough. Thanks in advance, Paul

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      One way to do this would be to use a ViewModel approach, and bind to a string property that aggregates this data. Consider the following sample:

      public class MyClass
      {
      private List<int> _myList = new List<int>();
      public string AggregateList
      {
      get
      {
      if (_myList != null && _myList.Count > 0)
      {
      StringBuilder myString = new StringBuilder();
      foreach (int value in _myList)
      {
      sb.AppendFormat("{0},", value);
      }
      return sb.ToString().Substring(0, sb.ToString().Length - 2);
      }
      return string.Empty;
      }
      set
      {
      string[] values = value.Split(",");
      _myList.Clear();
      foreach (string item in values)
      {
      int output;
      if (int.TryParse(item, out output))
      {
      _myList.Add(output);
      }
      }
      }
      }
      }

      You could do something like this using converters, but this is a simple method - and you just need to bind to this property. I've just knocked this up in the HTML editor here, so there may be a mistake or two, so I apologise in advance for them.

      "WPF has many lovers. It's a veritable porn star!" - Josh Smith

      As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

      My blog | My articles | MoXAML PowerToys | Onyx

      P 1 Reply Last reply
      0
      • P pbalaga

        Hi, I managed to bring to work simple data template in a ListView control:

        <DataTemplate>
        <TextBox Text="{Binding Path=boo}" />
        </DataTemplate>

        Class of the items added to the ListView contains property 'boo' of type string. But how to do similar binding for a custom complex type. Let's say instead of string boo, I have List<int> integers;. What I'm trying to achieve is to make the textbox display the numbers in the list separated with commas (e.g. 3,5,15,2). In other words, is it possible to do something like

        <TextBox Text="{Binding Path=integers}" />

        by providing how the target should be formatted? The main issue is that it should be a two-way binding (with ability to convert "1,5,4" back to List). What direction to go? I assume I could implement a string property, i.e. 'NumbersString' returning joined contents of the list in form of a single string. Or maybe it has something to do with converters? I only hope my explanation is clear enough. Thanks in advance, Paul

        A Offline
        A Offline
        Abhinav S
        wrote on last edited by
        #3

        You could put the textbox in a stackpanel and then set the stackpanel's orientation to horizontal.

        Me, I'm dishonest. And a dishonest man you can always trust to be dishonest.
        Honestly. It's the honest ones you want to watch out for...

        P P 2 Replies Last reply
        0
        • P Pete OHanlon

          One way to do this would be to use a ViewModel approach, and bind to a string property that aggregates this data. Consider the following sample:

          public class MyClass
          {
          private List<int> _myList = new List<int>();
          public string AggregateList
          {
          get
          {
          if (_myList != null && _myList.Count > 0)
          {
          StringBuilder myString = new StringBuilder();
          foreach (int value in _myList)
          {
          sb.AppendFormat("{0},", value);
          }
          return sb.ToString().Substring(0, sb.ToString().Length - 2);
          }
          return string.Empty;
          }
          set
          {
          string[] values = value.Split(",");
          _myList.Clear();
          foreach (string item in values)
          {
          int output;
          if (int.TryParse(item, out output))
          {
          _myList.Add(output);
          }
          }
          }
          }
          }

          You could do something like this using converters, but this is a simple method - and you just need to bind to this property. I've just knocked this up in the HTML editor here, so there may be a mistake or two, so I apologise in advance for them.

          "WPF has many lovers. It's a veritable porn star!" - Josh Smith

          As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

          My blog | My articles | MoXAML PowerToys | Onyx

          P Offline
          P Offline
          pbalaga
          wrote on last edited by
          #4

          Thank you for your time! This should be enough in some cases, including my current issue, but it seems somewhat less flexible. Let me be a bit curious, so what if an user wants to write his own style(?) and have the data separated with semicolons, not commas? May seem overcomplicated, but I'm asking, because it may happen that the end-user will be unable to change the data input. So the List<int> will be always a List<int> without a chance to wrap it as above. Thanks once again! Anyway I've just found a site about converter binding in xaml and I think it can be highly educational.

          P 1 Reply Last reply
          0
          • A Abhinav S

            You could put the textbox in a stackpanel and then set the stackpanel's orientation to horizontal.

            Me, I'm dishonest. And a dishonest man you can always trust to be dishonest.
            Honestly. It's the honest ones you want to watch out for...

            P Offline
            P Offline
            pbalaga
            wrote on last edited by
            #5

            I don't get the point. Did you mean adding multiple textboxes to that StackPanel?

            A 1 Reply Last reply
            0
            • P pbalaga

              I don't get the point. Did you mean adding multiple textboxes to that StackPanel?

              A Offline
              A Offline
              Abhinav S
              wrote on last edited by
              #6

              <ListBox x:Name="DemoList"> <ListBox.ItemsPanel> <ItemsPanelTemplate> <StackPanel Orientation="Horizontal" /> </ItemsPanelTemplate> </ListBox.ItemsPanel> <DataTemplate> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition> <ColumnDefinition> </Grid.ColumnDefinitions> </Grid> <TextBox grid.column=0 Text1="{Binding integers}"/> <TextBox grid.column=1 Text1=";"/> </DataTemplate> </ListBox> You might also want to take a look here[^].

              1 Reply Last reply
              0
              • P pbalaga

                Thank you for your time! This should be enough in some cases, including my current issue, but it seems somewhat less flexible. Let me be a bit curious, so what if an user wants to write his own style(?) and have the data separated with semicolons, not commas? May seem overcomplicated, but I'm asking, because it may happen that the end-user will be unable to change the data input. So the List<int> will be always a List<int> without a chance to wrap it as above. Thanks once again! Anyway I've just found a site about converter binding in xaml and I think it can be highly educational.

                P Offline
                P Offline
                Pete OHanlon
                wrote on last edited by
                #7

                You could use a converter if you wanted - if the user wants to write his own style though, you are faced with having to change the code anyway. The sample I posted was aimed at satisfying your problem - it's designed around POCO (Plain Old CLR Objects) which means that it's easy to test.

                "WPF has many lovers. It's a veritable porn star!" - Josh Smith

                As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

                My blog | My articles | MoXAML PowerToys | Onyx

                1 Reply Last reply
                0
                • A Abhinav S

                  You could put the textbox in a stackpanel and then set the stackpanel's orientation to horizontal.

                  Me, I'm dishonest. And a dishonest man you can always trust to be dishonest.
                  Honestly. It's the honest ones you want to watch out for...

                  P Offline
                  P Offline
                  Pete OHanlon
                  wrote on last edited by
                  #8

                  The original question was how could you wrap the list into one textbox, not how to display the list horizontally.

                  "WPF has many lovers. It's a veritable porn star!" - Josh Smith

                  As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

                  My blog | My articles | MoXAML PowerToys | Onyx

                  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