2016-11-11 25 views
0

我的数据网格给了我正确数量的行,但行中没有数据。Datagrid行是空的

这里是我的WPF代码

<Grid AllowDrop="True"> 
    <DataGrid x:Name="dataGrid" AutoGenerateColumns="False" ItemsSource="{Binding}"> 
     <DataGrid.Columns> 
      <DataGridTextColumn x:Name="c1" Header="Full Name" Binding="{Binding FullNames}" Width="200"/> 
      <DataGridTextColumn x:Name="c2" Header="Age" Binding="{Binding Ages}" Width="200"/> 
     </DataGrid.Columns> 
    </DataGrid> 
    <Button x:Name="button" Content="Button" HorizontalAlignment="Left" Margin="128,296,0,3" Width="75" Click="button_Click"/> 
</Grid> 

这背后是提前

public partial class MainWindow : Window 
{ 
    public string FullNames { get; set; } 
    public int Ages { get; set; } 

    public MainWindow() 
    { 
     InitializeComponent(); 
     this.dataGrid.DataContext = GetInfo(); 

    } 


    private List<string> GetInfo() 
    { 
     List<string> list = new List<string>(); 
     List<int> listAge = new List<int>(); 

     list.Add(FullNames = "User 1"); 
     list.Add(FullNames = "User 2"); 
     list.Add(FullNames = "User 3"); 
     list.Add(FullNames = "User 4"); 
     list.Add(FullNames = "User 5"); 
     listAge.Add(Ages = 35); 
     listAge.Add(Ages = 34); 
     listAge.Add(Ages = 10); 
     listAge.Add(Ages = 8); 
     listAge.Add(Ages = 4); 

     return list; 
    } 

} 

感谢我的代码。顺便说一句,我必须写这个,因为stackoverflow说我需要更多的细节。我认为我写的小代码是足够的,但我猜不是,大声笑

回答

0

当这些属性被分配到,以更新用户界面时没有得到更新:1)使用ObservableCollection,这是一个当项目被添加或删除时更新UI的列表。 2)定义一个类,如:

public class Person: DependencyObject 
{ 

    public string name 
    { 
     get { return (string)GetValue(nameProperty); } 
     set { SetValue(nameProperty, value); } 
    } 

    // Using a DependencyProperty as the backing store for name. This enables animation, styling, binding, etc... 
    public static readonly DependencyProperty nameProperty = 
     DependencyProperty.Register("name", typeof(string), typeof(Person), new PropertyMetadata("Fred")); 


    public int age 
    { 
     get { return (int)GetValue(ageProperty); } 
     set { SetValue(ageProperty, value); 
      if (fireshower != null) 
       fireshower.Value = value; 
     } 
    } 

    // Using a DependencyProperty as the backing store for age. This enables animation, styling, binding, etc... 
    public static readonly DependencyProperty ageProperty = 
     DependencyProperty.Register("age", typeof(int), typeof(RuleTileInfo), new PropertyMetadata(0)); 


} 

这确实2米你的东西:它会使你的代码更易于扩展,现在的信息是整齐的,如果一个人的年龄变化的UI将会收到有关更新。 3)虽然可以将datacontext设置为Observable集合并使用ItemsSource = {Binding},但最好将UI中显示的所有对象放入视图模型中,并将窗口的datacontext设置为视图模型。然后绑定到该视图模型中列表的名称。虚拟机的情况可能是:

public class WindowVM: DependencyObject 
{ 

public ObservableCollection<Person> People 
{ 
    get { return (string)GetValue(PeepleProperty); } 
    set { SetValue(PeopleProperty, value); } 
} 

// Using a DependencyProperty as the backing store for name. This enables animation, styling, binding, etc... 
//ect 

} 

这将帮助组织您的程序,并使其更容易理解和更易于维护。提示:手动输入依赖性属性很麻烦,请使用它们的片段(输入propdp并点击两次选项卡,填写选项卡以在它们之间切换的字段,VS将为您填写冗余信息)

+0

你为你的输入!对不起,这么晚回来。依赖属性比我的头大一点。我真的只是想了解WPF中的绑定。再次感谢 – jwill