views:

173

answers:

2

Hi everyone,

        var a1 = "HEL";
        var a2 = "HELLO";
        var a3 = "LLO";
        var length = a2.Length+5;

        listbox.Items.Add(a1.PadRight(length) +"End");
        listbox.Items.Add(a2.PadRight(length) + "End");
        listbox.Items.Add(a3.PadRight(length) + "End");

I have code like this to obviously pad all text so that the word End lines up.

The problem is I have to change the font from the wpf listbox from Segoe UI to Courier New to have this work. The rest of my app uses Segoe UI, so I think it looks weird here.

Is there any way to achieve the result with Segoe UI or maybe a similar font with correct spacing I could use, or maybe someone has some other smart solution i haven't even thought of? :-)

Thanks

edit

at the end of the day I want this to display to related items like this:

ITEM A    -> ITEM B
ITEM X    -> ITEM Y
ITEM C    -> ITEM E

dont want to use gridview.

A: 
<ListBox
     x:Name="listBox" HorizontalContentAlignment="Right"/>

check it :)

Aran Mulholland
it doesn't do what I want? but maybe i didn't explain properly i will make an edit
baron
+1  A: 

Feed the ListBox the two pieces of data separately, and use a data template. Here's how.

First, create a little class to represent each item you want to insert:

public class WordPair {
  public string First { get; set; }
  public string Second { get; set; }
}

(You probably already have a suitable class and/or collection in your application -- I assume those pairs of strings are coming from somewhere!)

Second, set your ListBox.ItemsSource to a collection of these things:

listBox.ItemsSource = new List<WordPair> {
  new WordPair { First = "ITEM A", Second = "ITEM B" },
  new WordPair { First = "ITEM X", Second = "ITEM Y" },
};

Again, this collection may already exist in your app.

Third, create a DataTemplate specifying the desired layout, and assign it to your ListBox.ItemTemplate:

<!-- in your Window.Resources section -->
<DataTemplate x:Key="AlignedPairs">
  <Grid>
    <Grid.ColumnDefinitions>
      <ColumnDefinition Width="*" />
      <ColumnDefinition Width="Auto" />
      <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>
    <TextBlock Text="{Binding First}" Grid.Column="0" />
    <TextBlock Text="->" TextAlignment="Center" Grid.Column="1" />
    <TextBlock Text="{Binding Second}" TextAlignment="Right" Grid.Column="2" />
  </Grid>
</DataTemplate>

<ListBox Name="listBox" ItemTemplate="{StaticResource AlignedPairs}">
  <ListBox.ItemContainerStyle>
    <Style TargetType="ListBoxItem">
      <Setter Property="HorizontalContentAlignment" Value="Stretch" />
    </Style>
  </ListBox.ItemContainerStyle>
</ListBox>

(I've guessed at the exact alignment you want for the items, but you can obviously tweak it.)

Note that you also need to set the HorizontalContentAlignment of the ListBoxItems to Stretch using ListBox.ItemContainerStyle. Otherwise each ListBoxItem will take up only the space it needs, resulting in all the Grid columns being minimal size and looking like a straight concatenation. Stretch makes each ListBoxItem fill the full width so the Grid columns are forced to grow accordingly.

itowlson
Did you test this? This looked very promising, but I couldn't find anyway to set HorizontalAlignment or HorizontalContentAlignment through Listbox.ItemContainerStyle, and using exactly what you have put gives no formatting at all. That produces what this would: listbox.Items.Add("ITEM 1 " + "->" + " ITEM 2"); etc etc
baron
I've updated with the required ItemContainerStyle. This makes it work in my test environment (note that I've assumed ITEM 2 is to be aligned right and you may still need to tweak it if you want a different alignment).
itowlson
Thanks very much, this is a perfect solution. I have one more question, How could I go about adding a title listitem. i.e. before I had listbox.Add("First Name " + " Last Name"); but now setting Itemssource I can't do it. Is there anyway other than adding an object into the collection that acts as the title? i.e. Firstname = "FIRST NAME" and Secondname = "SECOND NAME" ? Maybe I just set a label/textblock above the listbox
baron
Yes, putting a caption above the ListBox is one way. Another is to create a custom ControlTemplate for the ListBox, and put the caption in that ControlTemplate -- though this is probably overkill unless this is intended to be a reusable component. Adding a "special" item to the ListBox is less desirable because the special item is selectable and that isn't really appropriate (this is a consideration even if you use Items.Add). You might want to raise this as a separate question so as to get more answers though (it may already have been answered elsewhere on SO!).
itowlson
Went with the caption above the listbox, it looks good enough : ) Cheers
baron