Grid in a combo box [modified]
-
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 b3Items 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 aList<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
-
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 b3Items 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 aList<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
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
-
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
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
-
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
I'd pass on this implementation. Too much hard coding of everything.
-
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
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...
-
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...
-
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
You're welcome. BTW... forgot to mention the bold thing... you should use the ListBox AlternationCount for the bold feature.
-
You're welcome. BTW... forgot to mention the bold thing... you should use the ListBox AlternationCount for the bold feature.