2011-10-21 6 views
0

모델에서 바인딩 된 항목을 목록 상자에 어떻게 정렬합니까?WPF order listbox items

나는이 모델 정의 : 나는 모든 알림 항목에 할당 된 ID로 그것을 주문해야

public ObservableCollection<NotificationItem> Classes: 

합니다. 현재

나는이 정의 : I 모델의 참조가

<ListBox ItemsSource="{Binding Classes, Source={StaticResource model}}" 
        ScrollViewer.VerticalScrollBarVisibility="Visible" 
DataContext="{Binding}"> 
         <ListBox.ItemTemplate> 
          <DataTemplate> 
           <StackPanel Orientation="Horizontal"> 
            <Label Content="{Binding Name}" /> 
           </StackPanel> 
          </DataTemplate> 
         </ListBox.ItemTemplate> 
        </ListBox> 

:

<Model:ClassModel x:Key="model" /> 

업데이트 : 모델의 초기화가

try 
      { 
       this.notifierModel = this.Resources["model"] as ClassModel; 

       this.classController.Initialize(this.notifierModel); 

      } 
      catch 
      { 
      // todo: handle exception 
      } 
에 의해 이루어집니다

답변

3

모델 (데이터 컨텍스트)에서 컬렉션을 직접 주문하거나 CollectionViewSource 사용자 지정을 만들고 대신 ListBox를 바인딩 할 수 있습니다. 당신이 Classes을 설정

+0

thnx 마침내 나는 어떻게이 일을 다우 이해 ... thnx – cpoDesign

3

당신이 필요합니다 :

대신의
Classes = new ObservableCollection(results.OrderBy(i => i.Id)); 

:

Classes = new ObservableCollection(results); 

results 귀하의 질의에 의해 반환 된 데이터의 목록입니다 가정.

+0

크리스, 당신의 솔루션을 작동하게 만들 수 없습니다. 나는 그 목록이 처음부터 비어 있기 때문에 그것이 생각한다. 그리고이 솔루션은 이미 완료된 컬렉션에만 작동합니다. 그게 맞습니까, 아니면 제가 빠진 것이 있습니까? – cpoDesign