2013-08-09 3 views
0

데이터 그리드가 있고이 예에서는 하나의 열만 있습니다.DataGridTemplateColumn을 ComboBox로 정렬

<DataGrid x:Name="grdMainGrid"> 
    <DataGridTemplateColumn Header="Room" CanUserSort="True" SortMemberPath="DisplayText" > 
     <DataGridTemplateColumn.CellTemplate> 
      <DataTemplate> 
       <ComboBox ItemsSource="{Binding Path=AllRooms, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}}" Height="20" SelectedValuePath="Code" SelectedValue="{Binding Path=RoomCode, UpdateSourceTrigger=PropertyChanged}" DisplayMemberPath="DisplayText" /> 
      </DataTemplate> 
     </DataGridTemplateColumn.CellTemplate> 
    </DataGridTemplateColumn> 
</DataGrid> 

데이터 격자의 ItemsSource가 목록으로 설정 :이 열은 DataGridTemplateColumn입니다

DataGridTemplateColumn의 콤보 상자의 ItemsSource가 내 윈도우의 속성에 바인딩
public class InsertableRecord 
{ 
    public int RoomCode { get; set; } 
} 

:

public List<Room> AllRooms 
{ 
    get; 
    private set; 
} 

다음은 "방"클래스의 정의입니다.

public partial class Room 
{ 
    public string ID { get; set; } 

    public string Description { get; set; } 

    public string DisplayText 
    { 
     get 
     { 
      return this.ID + " (" + this.Description + ")"; 
     } 
    } 
} 

"ReservableRecord"가 아닌 "Room"의 속성 인 DisplayText로 설정된 SortMemberPath를 갖게됩니다. 그래서 분명히, "DisplayText"속성이 "InsertableRecord"개체에 존재하지 않는다는 것을 말하는이 열을 정렬하려고하면 바인딩 오류가 발생합니다.

현재 ComboBox의 텍스트 (또는 Room 개체의 DisplayText 속성 둘 다 작동합니다)를 기준으로 열을 정렬하는 방법은 무엇입니까?

답변

0

좋아, temporarly, 작은 해킹을 만들었습니다 : SelectedDisplayText라는 InsertableRecord 안에 새로운 속성을 만들었습니다.

public class InsertableRecord 
{ 
    public int RoomCode { get; set; } 
    public string SelectedDisplayText { get; set; } 
} 

나는 다음에 내 데이터 그리드 열 정의를 변경 :

<DataGrid x:Name="grdMainGrid"> 
    <DataGridTemplateColumn Header="Room" CanUserSort="True" **SortMemberPath="SelectedDisplayText"** > 
     <DataGridTemplateColumn.CellTemplate> 
      <DataTemplate> 
       <ComboBox ItemsSource="{Binding Path=AllRooms, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}}" Height="20" SelectedValuePath="Code" SelectedValue="{Binding Path=RoomCode, UpdateSourceTrigger=PropertyChanged}" DisplayMemberPath="DisplayText" **Text="{Binding Path=SelectedDisplayText, Mode=OneWayToSource}"** /> 
      </DataTemplate> 
     </DataGridTemplateColumn.CellTemplate> 
    </DataGridTemplateColumn> 
</DataGrid> 

그리고 지금이와 함께, 때마다 나는 내 콤보 박스의 선택에 채워됩니다 콤보 상자의 새로운 "선택한 텍스트"로 변경 InsertableRecord 개체의 "SelectedDisplayText"및 DataGrid는이 값을 사용하여 해당 값에 따라 정렬 할 수 있습니다.

이제는 작동하지만 여전히 해킹 된 것처럼 느껴집니다. 내가 어떻게 든, 그 행의 관련 ComboBox를 얻을 수 있고 현재의 텍스트 값을 추출 할 수있는 행의 데이터 컨텍스트를 통해 사용자 정의 정렬을 구축 할 수있는 방법을 제외하고 싶습니다 ...하지만 그 doesn 선택의 여지가없는 것 같아요 ...

다른 제안은 내가 내 응용 프로그램을 통해 재사용 할 패턴이므로 이해할 수 있습니다. 가능한 한 깨끗하게 만들어야합니다. 반복 코드를 다시 작성해야하는 ...

관련 문제