【问题标题】:WPF Binding Complex ObjectWPF 绑定复杂对象
【发布时间】:2016-03-10 19:17:14
【问题描述】:

我有一个实现INotifyPropertyChanged 的模型,如下所示。将名称和城市绑定到标签很容易。但我无法将玩家绑定到数据网格。有没有人有任何提示? (这都是只读信息。)

public class Team : INotifyPropertyChanged
{
    private string _name;
    private string _city;
    private List<Player> _players;

    public string Name
    {
        get { return _name; }
    }

    public string City
    {
        get { return _city; }
    }

    public List<Player> Players
    {
        get { return _players; }
    }

}
public class Player
{
    public int ID {get; set;}
    public string FirstName {get; set;}
    public string LastName {get; set;}
}

【问题讨论】:

  • 请为您的DataGrid 显示您的 XAML 代码。你试过什么?
  • 你不能编译这个,我编辑了你的代码,看看我的答案

标签: c# wpf data-binding datagrid


【解决方案1】:

谁知道我以为我在做什么。但它现在正在工作。这是ItemSource:

ItemsSource="{Binding Path=Players}"

【讨论】:

    【解决方案2】:

    更改您的 Team 类,您必须像这样实现 INotifypropertyChanged:

      public class Team : INotifyPropertyChanged
        {
            private string _name;
            private string _city;
            private List<Player> _players;
    
            public string Name
            {
                get { return _name; }
            }
    
            public string City
            {
                get { return _city; }
            }
    
            public List<Player> Players
            {
                get { return _players; }
                set { 
                    _players =value;
                    RaisePropertyChanged("Players");
                }
            }
    
    
            #region INotifyPropertyChanged
    
            internal void RaisePropertyChanged(string prop)
            {
                if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(prop)); }
            }
            public event PropertyChangedEventHandler PropertyChanged;
    
            #endregion
    
        }
        public class Player
        {
            public int ID { get; set; }
            public string FirstName { get; set; }
            public string LastName { get; set; }
        }
    

    通过 Team 类的实例设置 MainWindow 的 DataContext,如下所示:

       public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
                DataContext = new Team
                {
                    Players = new List<Player>()
                    { 
                        new Player{ ID=1, FirstName="Mohammed", LastName="Ali" },
                        new Player{ ID=2, FirstName="Paul", LastName="Oshain" },
                    }
                };
            }
        }
    

    最后在您的 XAML(您的 MainWindow)中进行良好的绑定;像这样:

    <Window x:Class="WpfApplication4.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="350" Width="525">
        <Grid>
            <DataGrid ItemsSource="{Binding Players}" AutoGenerateColumns="False" >
                <DataGrid.Columns>
                    <DataGridTextColumn Header="Id"  Binding="{Binding ID}"/>
                    <DataGridTextColumn Header="First Name"  Binding="{Binding FirstName}"/>
                    <DataGridTextColumn Header="Last Name" Binding="{Binding LastName}" />
              </DataGrid.Columns>
            </DataGrid>
        </Grid>
    </Window>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-02-04
      • 2011-10-02
      • 1970-01-01
      • 1970-01-01
      • 2011-02-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多