简体   繁体   中英

Cannot fill a listbox gridcolumns with data from a list, wpf

In this hobby project I want to fill a wpf listbox. I have a similar listbox where there is no problem in showing the data formatted in columns, so I have copied the XAML and the code, but the next listbox shows nothing. If I remove the formatting in grid columns I can fill the listbox with data. So the source list is ok. But the formatting in columns must be the problem.

I have a class Rounds, in which I have a method for adding data.

I have the XAML and the code.

What is wrong?

    Class:
    namespace BettingPoker
    {
    internal class Rounds 
    {
    private string  RoundName { get; set; }
    private int RoundNumber { get; set; }

   

    public Rounds(string roundname,  int roundnumber)

    {
        this.RoundName = roundname;
        this.RoundNumber = roundnumber;
      
}
    public static List<Rounds> roundList = new List<Rounds>();

    public void TilføjTilRound(string name, int number)
    {

        roundList.Add(new Rounds(name, number));

    }


    public override string ToString()
    {
        if (RoundName != null)
            return RoundName + "  " + RoundNumber;
        return "Problem";
    }


   }
   }

   XAML:

   <ListBox x:Name="listBoxRunder" Height="135" Margin="478,244,170,0" 
   VerticalAlignment="Top" ItemsSource="{Binding}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Grid Margin="0,2">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="10"></ColumnDefinition>
                        <ColumnDefinition Width="30"></ColumnDefinition>
                        <ColumnDefinition Width="2"></ColumnDefinition>
                        <ColumnDefinition Width="30"></ColumnDefinition>
                        <ColumnDefinition Width="2"></ColumnDefinition>
                      
                    </Grid.ColumnDefinitions>

                    <TextBox Text="{Binding RoundName}" Grid.Column="1"></TextBox>
                    <TextBox Text="{Binding RoundNumber}" Grid.Column="3"></TextBox>
                    
                </Grid>
            </DataTemplate>
          </ListBox.ItemTemplate>
        </ListBox>

        CS code:
        Rounds runde = new Rounds("", 0);
        runde.TilføjTilRound("Blinds", 0);
        runde.TilføjTilRound("Preflop", 1);
        runde.TilføjTilRound("Flop", 2);
        runde.TilføjTilRound("Turn", 3);
        runde.TilføjTilRound("River", 4);

        listBoxRunder.ItemsSource = Rounds.roundList;

These are properties:

private string  RoundName { get; set; }
private int RoundNumber { get; set; }

They're currently only accessible to your Rounds class because they are set to private, meaning, only Rounds can read and write to those properties. In XAML, you're trying to access those properties but they do not exist. I suspect you're getting some sort of warning in your debugger hinting at that.

I'm not entirely sure, but since your class is set to internal that may also be contributing to the problem. When I first started programming, I almost without fail set everything to public until I better understood how to use access modifiers to my advantage. I encourage you to read up on them here where this article talks about access modifiers for variables, methods, and classes inclusively.
https://learn.microsoft.com/en-us/do.net/csharp/programming-guide/classes-and-structs/access-modifiers

Another aspect of the code you should change is how you have your properties set up, you should make use of private fields and public properties. In your code you always call on the properties directly, and the fields are only for use inside the properties - this enables you to call the OnPropertyChanged() method which updates the UI dynamically whenever the property value changes.

try this:

    namespace BettingPoker
    {
      //set class to public and implement INotifyPropertyChanged
      public class Rounds : INotifyPropertyChanged
      {
         /*
          these are called fields.  Use them in your properties to make 
          use of OnPropertyChanged();
          */
         private string _roundName, roundNumber;
         
         public string  RoundName
         { 
           get return _roundName; 
           set _roundName = value; OnPropertyChanged(); 
         }
         public int RoundNumber 
         { 
           get return _roundNumber; 
           set _roundNumber = value; OnPropertyChanged(); 
         }
      }//close class
  }//close namespace

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