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. Grid in a combo box [modified]

Grid in a combo box [modified]

Scheduled Pinned Locked Moved WPF
tutorialcssdatabasequestion
8 Posts 3 Posters 1 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.
  • L Offline
    L Offline
    Lutoslaw
    wrote on last edited by
    #1

    Hello again! I have searched for it but couldn't find an apropriate solution. I have a list of objects, and I want to arrange them in rows, but still allowing user to select any individual item. For example:

    list = { a1, a2,a3,b1,b2,b3}

    In a combobox I would like to see:

    [ ]V
    a1 a2 a3
    b1 b2 b3

    Items in a combobox are enumeration members and value of a combobox should be bound. User can select any value from the list. Moreover, items a2,b2,c2,... (that is, when index%3==1) should appear in bold (extra feature). I have tried to set a List<List<MyClass>> as a datasource, but in that case user would be able to set a whole row and not individual items. Such behaviour is unacceptable. How to accomplish this? Thanks in advance,

    Greetings - Jacek

    modified on Sunday, July 31, 2011 6:56 PM

    T 1 Reply Last reply
    0
    • L Lutoslaw

      Hello again! I have searched for it but couldn't find an apropriate solution. I have a list of objects, and I want to arrange them in rows, but still allowing user to select any individual item. For example:

      list = { a1, a2,a3,b1,b2,b3}

      In a combobox I would like to see:

      [ ]V
      a1 a2 a3
      b1 b2 b3

      Items in a combobox are enumeration members and value of a combobox should be bound. User can select any value from the list. Moreover, items a2,b2,c2,... (that is, when index%3==1) should appear in bold (extra feature). I have tried to set a List<List<MyClass>> as a datasource, but in that case user would be able to set a whole row and not individual items. Such behaviour is unacceptable. How to accomplish this? Thanks in advance,

      Greetings - Jacek

      modified on Sunday, July 31, 2011 6:56 PM

      T Offline
      T Offline
      teejayem
      wrote on last edited by
      #2

      From MSDN[^] - A ListBoxItem is a ContentControl, which means that it can contain a single object of any type (such as a string, an image, or a panel). This means you can directly add any type of content you wish (like a grid).

              Grid grid = new Grid();
              grid.Children.Add(new TextBlock { Text = "Hello"});
      
              listBox.Items.Add(grid);
      

      Or you can use a grid in your ItemTemplate

              listBox.ItemsSource = new MyClass\[\] {
                  new MyClass { Property1 = "a1", Property2 = "a2", Property3 = "a3"},
                  new MyClass { Property1 = "b1", Property2 = "b2", Property3 = "b3"},
                  new MyClass { Property1 = "c1", Property2 = "c2", Property3 = "c3"},
              };
      

      and the FontWeightConverter

      public class FontWeightConverter : IValueConverter
      {
          public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
          {
              String item = value as String;
              if (item == null) return FontWeights.Normal;
      
              int index = int.Parse(item.Substring(1, 1));
      
              return index % 3 == 1 ? Fon
      
      L S 2 Replies Last reply
      0
      • T teejayem

        From MSDN[^] - A ListBoxItem is a ContentControl, which means that it can contain a single object of any type (such as a string, an image, or a panel). This means you can directly add any type of content you wish (like a grid).

                Grid grid = new Grid();
                grid.Children.Add(new TextBlock { Text = "Hello"});
        
                listBox.Items.Add(grid);
        

        Or you can use a grid in your ItemTemplate

                listBox.ItemsSource = new MyClass\[\] {
                    new MyClass { Property1 = "a1", Property2 = "a2", Property3 = "a3"},
                    new MyClass { Property1 = "b1", Property2 = "b2", Property3 = "b3"},
                    new MyClass { Property1 = "c1", Property2 = "c2", Property3 = "c3"},
                };
        

        and the FontWeightConverter

        public class FontWeightConverter : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                String item = value as String;
                if (item == null) return FontWeights.Normal;
        
                int index = int.Parse(item.Substring(1, 1));
        
                return index % 3 == 1 ? Fon
        
        L Offline
        L Offline
        Lutoslaw
        wrote on last edited by
        #3

        Thanks for your answer! Unfortunately, I don't see how user could select, let say, "a3" in a grid without selecting "a1" and "a2". The list of possible values is flat, and the selection is bound to a single property. User has to choose a single item. Arranging items in a grid is a layout/GUI issue and does not reflect application logic. Less important: let's say that ther are n columns in a grid. The solution which you have kindly provided fixes n=3. Not so bad since the number of columns will be fixed, but IMHO it is risky and not easily scalable.

        Greetings - Jacek

        S 1 Reply Last reply
        0
        • T teejayem

          From MSDN[^] - A ListBoxItem is a ContentControl, which means that it can contain a single object of any type (such as a string, an image, or a panel). This means you can directly add any type of content you wish (like a grid).

                  Grid grid = new Grid();
                  grid.Children.Add(new TextBlock { Text = "Hello"});
          
                  listBox.Items.Add(grid);
          

          Or you can use a grid in your ItemTemplate

                  listBox.ItemsSource = new MyClass\[\] {
                      new MyClass { Property1 = "a1", Property2 = "a2", Property3 = "a3"},
                      new MyClass { Property1 = "b1", Property2 = "b2", Property3 = "b3"},
                      new MyClass { Property1 = "c1", Property2 = "c2", Property3 = "c3"},
                  };
          

          and the FontWeightConverter

          public class FontWeightConverter : IValueConverter
          {
              public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
              {
                  String item = value as String;
                  if (item == null) return FontWeights.Normal;
          
                  int index = int.Parse(item.Substring(1, 1));
          
                  return index % 3 == 1 ? Fon
          
          S Offline
          S Offline
          SledgeHammer01
          wrote on last edited by
          #4

          I'd pass on this implementation. Too much hard coding of everything.

          1 Reply Last reply
          0
          • L Lutoslaw

            Thanks for your answer! Unfortunately, I don't see how user could select, let say, "a3" in a grid without selecting "a1" and "a2". The list of possible values is flat, and the selection is bound to a single property. User has to choose a single item. Arranging items in a grid is a layout/GUI issue and does not reflect application logic. Less important: let's say that ther are n columns in a grid. The solution which you have kindly provided fixes n=3. Not so bad since the number of columns will be fixed, but IMHO it is risky and not easily scalable.

            Greetings - Jacek

            S Offline
            S Offline
            SledgeHammer01
            wrote on last edited by
            #5

            Actually, a much simpler implementation would be to just swap out the item panel for a WrapPanel. Then you could select individual items... Simple as 1-2-3...

            L 1 Reply Last reply
            0
            • S SledgeHammer01

              Actually, a much simpler implementation would be to just swap out the item panel for a WrapPanel. Then you could select individual items... Simple as 1-2-3...

              L Offline
              L Offline
              Lutoslaw
              wrote on last edited by
              #6

              This is what I needed! Thanks very much! You have a knowledge of the framework. In this specific scenario, the exact solution is:

              <WrapPanel ItemWidth="45" Width="225" />

              which gives exactly 5 items in a row, as requested.

              Greetings - Jacek

              S 1 Reply Last reply
              0
              • L Lutoslaw

                This is what I needed! Thanks very much! You have a knowledge of the framework. In this specific scenario, the exact solution is:

                <WrapPanel ItemWidth="45" Width="225" />

                which gives exactly 5 items in a row, as requested.

                Greetings - Jacek

                S Offline
                S Offline
                SledgeHammer01
                wrote on last edited by
                #7

                You're welcome. BTW... forgot to mention the bold thing... you should use the ListBox AlternationCount for the bold feature.

                L 1 Reply Last reply
                0
                • S SledgeHammer01

                  You're welcome. BTW... forgot to mention the bold thing... you should use the ListBox AlternationCount for the bold feature.

                  L Offline
                  L Offline
                  Lutoslaw
                  wrote on last edited by
                  #8

                  OK I will try it.

                  Greetings - Jacek

                  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