简体   繁体   中英

List not displaying correctly WPF

Situation

Hey guys, for a program I'm developing I have a getRaces() method:

public string getAllBaseRaces()
        {
            //string to hold a list of members
            string strRaces = "";


            foreach (BaseRace s in races)
            {
                strRaces = strRaces + s.ToString() + "\n";
            }


            return strRaces;
        }

I'm trying to populate a list with my getRaces( ) methods return, which is a string, but when I do that I get something that looks like this:

this.DataContext = hillracing.getAllBaseRaces();

http://imgur.com/X1GdB52

The contents of the listbox are correct, it displays all of my parameters, like the name of the race, the ID of the Race, the Type of the race, it also shows all races as it's meant too, that part is fine.

Problem

It seems as if when the string is displaying, it's displaying incorrectly, as it's storing each character as a separate list item, rather than each Race being Seperate list item.

I've looked online and solutions are vague and don't really fit my scenario specifically.

However

when I just show the List in my Hillracing class (Which Stores Race Objects) I get something like this:

this.DataContext = hillracing.Races;

http://imgur.com/PD3KFMn

--Obviously, the second example image is the better of the two, and it's what I'm trying to achieve with the getRaces() method but I've failed at doing so, so I'm temporarily using this so I can work around it, the reason I can't use this as a permanent solution is because I don't have a list for All Member types, just BaseMember and I don't have a List for all Races, just BaseRace.

In short, With getRaces() as it is currently, It's not incredibly helpful since it's output is strange, I wondered how I could achieve the second image by using the getRaces() method.

XAML for list

<Grid Background="#19535353" Margin="-5,-3,-4,-4">
                    <Button Content="Create a Race" HorizontalAlignment="Left" Margin="56,163,0,0" VerticalAlignment="Top" Width="110" Height="110"/>
                    <Button x:Name="getRacesButton" Content="Get All Races" HorizontalAlignment="Left" Margin="56,30,0,0" VerticalAlignment="Top" Width="110" Height="110" Click="getRacesButton_Click_1"/>
                    <ContentControl Content="{Binding hillracing}" HorizontalAlignment="Left" Margin="326,273,0,0" VerticalAlignment="Top"/>
                    <ListBox HorizontalAlignment="Left" Height="406" Margin="287,30,0,0" VerticalAlignment="Top" Width="335" ItemsSource="{Binding hillracing}"/>
                    <Button Content="Join Selected Race" HorizontalAlignment="Left" Margin="287,441,0,0" VerticalAlignment="Top" Width="156" Height="42"/>
                    <Button Content="Edit Selected Race" HorizontalAlignment="Left" Margin="469,441,0,0" VerticalAlignment="Top" Width="153" Height="42" RenderTransformOrigin="0.34,0.548"/>
                </Grid>

Thanks guys.

You are binding to ItemsSource on ListBox . This expects multiple items, but you are binding a single string .

string can be treated as an IEnumerable<char> , so an item is generated for each character in that string . This is why you're seeing one character per line.

You should be binding to a more useful IEnumerable<T> where T represents each item. If you wanted to stick with string, something like:

public IEnumerable<string> GetRaces()
{
    foreach (BaseRace race in races)
    {
        yield return race.ToString();
    }
}

That said, I don't see what you're trying to do with this method. Why not just bind to Races instead (like your second example)?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM