2016-11-11 2 views
0

내 DataGrid에서 정확한 양의 행을 제공하지만 행에 데이터가 없습니다.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

UI를 업데이트하기 위해 UI가 업데이트되지 않았습니다. 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) Observable 컬렉션에 datacontext를 설정하고 ItemsSource = {Binding}을 사용하는 것이 좋지만 UI에 표시된 모든 객체를 뷰 모델에 배치하고 윈도우의 datacontext를 뷰 모델로 설정하는 것이 좋습니다 . 그런 다음 해당 뷰 모델의 목록 이름에 바인딩하십시오. thsi 경우의 VM은 다음과 같을 수 있습니다.

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

관련 문제