Reputation: 211
I am binding values to list box but string not showing in proper format.
ex my strings like
abc 10
abcd 20
asdfas 30
I made fixed length to first string that is 30 using padding
str1.PadRight(30) + str2.PadRight(2)
then also I am getting values like that
abc 10
abcd 20
asdfas 30
Please suggest how can I put same space between string
Upvotes: 2
Views: 1838
Reputation: 52371
I would create a ViewModel wrapping the text and a number:
class MyViewModel
{
public string Text { get; set; }
public int Number { get; set; }
}
Then give the ListBox
an ItemTemplate
that does the layout:
<ListBox>
<ListBox.ItemTemplate>
<DataTemplate>
<DockPanel>
<TextBlock DockPanel.Dock="Left"
Text="{Binding Text}"/>
<TextBlock DockPanel.Dock="Right"
Text="{Binding Number}"/>
</DockPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Upvotes: 0
Reputation: 91845
Windows Forms ListBox
supports multiple columns. There's an example right there in the documentation for the constructor.
Although this might not work with data binding. In which case, you'll probably need to use a ListView
instead.
Upvotes: 0
Reputation: 57902
The Windows User Interface usually uses a variable-width font, so you can't just pad with spaces to a given column to get the text to line up.
You will either need to set the ListBox to use a monospaced font (Courier or Lucida Console for example), use a ListView (or similar) that supports columns, or implement owner-drawn items so that you can control how the items are displayed, splitting up the text to drawing it in columns.
Upvotes: 2