2012-11-25 4 views
1

열 (행)이라는 ObservableCollection (MyRow 형식)이 있다고 가정 해 보겠습니다. MyRow에는 ObservableCollection (MyRowContents 유형) (내용)이라는 속성이 있습니다. 기본적으로 해당 행에 포함될 모든 내용의 목록입니다. MyRowContents에는 DisplayContents (string) 등의 여러 속성이 있습니다.복잡한 개체를 DataGrid에 바인딩

예를 들어 ... 2 행의 DataGrid와 3 열의 데이터 구조는 2 개의 "행"이 될 것이며 각각에는 3 개의 "내용"이 포함됩니다.

셀 내용에 행 [i] .Contents [j] .DisplayContents가 표시되도록하려면 XAML의 DataGrid에서 수행해야하는 작업은 무엇입니까?

편집 :

안녕, 초기 메시지에 분명히 충분히되지 않는 죄송합니다. 나는 데이터를 동적으로 표시 할 수있는 데이터 그리드를 만들기 위해 노력하고있다 (미리 알 수없는 데이터베이스 테이블에서).

가이드로 사용 : http://pjgcreations.blogspot.com.au/2012/09/wpf-mvvm-datagrid-column-collection.html - 저는 열을 동적으로 표시 할 수 있습니다.

<DataGrid ItemsSource="{Binding Rows}" AutoGenerateColumns="False" 
       w:DataGridExtension.Columns="{Binding ElementName=LayoutRoot, Path=DataContext.Columns}" /> 

어떻게 행 데이터를 동적으로 표시할지 모르겠습니다.

MyRow은 다음과 같습니다

public class MyRow 
{ 
    public int UniqueKey { get; set; } 
    public ObservableCollection<MyRowContents> Contents { get; set; } 
} 

MyRowContents은 다음과 같습니다

public class MyRowContents 
{ 
    public string ColumnName { get; set; } // Do I need this? 
    public object DisplayContents { get; set; } // What I want displayed in the grid cell 
} 

내가 내보기 모델, 당신이 볼 수있는 공유 재산 ObservableCollection에 행이되어 나는 ItemsSource에 바인딩하고있는 무슨 . 그러나 각 MyRowContents의 "DisplayContents"를 그리드보기의 해당 셀에 표시하는 방법을 모르겠습니다.

열 이름을 동적으로 표시 할 수 있다고 가정하면 다음과 같은 가정을 할 수 있습니다. 내용 (MyRow에서)의 항목 순서가 열의 순서와 일치합니다.

감사

그렇게 앱 논리가 비슷해야 RowContent에 행 ID를 넣어 모두의
+0

제목을 편집했습니다. "[제목에"태그 "가 포함되어 있어야합니까?] (http://meta.stackexchange.com/questions/19190/)"합의가 "아니오, 그렇지 않아야합니다"로 표시되어야합니다. –

+0

몇 가지 코드를 게시 할 수 있습니까? 설명은 클래스 구조와 너무 비슷합니까? –

+0

예, 초기 메시지를 업데이트했습니다. 감사. – theqs1000

답변

1

첫째 :

RowContent.cs :

public class RowContent 
{ 
    //public string ColumnName { get; set; } "you dont need this property." 

    public int ID; //You should put row ID here. 
    public object DisplayContents { get; set; } "What is this property type, is string or int or custom enum??" 
} 

RowViewModel.cs :

public class RowViewModel 
{ 
    public ObservableCollection<MyRowContents> Contents { get; set; } 
} 

이제 모음을 Window.DataContext에 저장해야 i를 바인딩 할 수 있습니다. 데이터 그리드에서 t

MainWindow.xaml.cs를 :

public partial class MainWindow : Window 
{ 
    private RowViewModel rvm = new RowViewModel(); //Here i have made new instance of RowViewModel, in your case you have to put your ViewModel instead of "new RowViewModel();"; 

    public MainWindow() 
    { 
     InitializeComponent(); 

     DataContext = rvm; // this is very important step. 
    } 
} 

마지막 단계는 윈도우 XAML입니다.

MainWindow.xaml은 :은 DataGridColumn의 다섯 개 종류가있다

<DataGrid ItemsSource="{Binding Path=Contents}" AutoGenerateColumns="false"> 
    <DataGrid.Columns> 
     <DataGridTextColumn Header="Row ID" Binding="{Binding Path=ID,UpdateSourceTrigger=PropertyChanged}"/> 
     <DataGridTextColumn Header="your row header" Binding="{Binding Path=DisplayContents,UpdateSourceTrigger=PropertyChanged}"/> 
    </DataGrid.Columns> 
</DataGrid> 

, 당신은 DisplayContents 속성의 유형에 따라 적합한 유형을 선택해야합니다.

+0

열 머리글을 변경하거나 클래스 (MyClass의 경우)에 사용자 정의 유형이있는 경우 길을 설명해주십시오. 행운을 빕니다 – MoHaKa

+0

안녕하세요, 귀하의 회신에 감사드립니다. 미안하지만 초기 메시지가 충분히 명확하지 않아 초기 메시지를 더 자세히 업데이트했습니다. – theqs1000

+0

문제 없음, 답장을 보내 드리겠습니다. – MoHaKa

관련 문제