2013-01-08 3 views
0

콤보 상자에서 바인딩 된 (뷰 모델에서) 개체가 콤보 상자에서 항목이 선택 되 자마자 업데이트된다는 요구 사항이 내 프로그램에 있습니다. 현재 오브젝트는 Enter 키를 누르거나 셀을 나가면 편집이 커밋 된 후에 만 ​​업데이트됩니다. 사용자는 추가 단계를 원하지 않습니다..NET 4.0 DataGridCombobox SelectionChanged 문제

제 생각에는 콤보 상자에서 항목을 선택하면 CommitEdit() 메서드가 실행되고 CancelEdit()가 트리거됩니다. 그러나 DataGridComboBoxColumn에 대한 SelectionChanged 이벤트를 사용할 수 없으므로 연결할 방법을 찾지 못하는 것 같습니다.

다른 제안은 viewmodel에서 속성 변경 이벤트를 수신하는 것이지만 셀 편집이 끝날 때까지 속성은 변경되지 않습니다.

누구나 DataGridCombobox에서 새 항목 (색인)을 선택하여 사용자가 Enter 키를 누르거나 셀을 떠난 것처럼 셀 편집을 닫을 수있는 방법을 생각할 수 있습니까?

참고 : 고객 제한으로 인해 .NET 4.5를 사용할 수 없습니다.

답변

1

비슷한 문제가 있었지만 방금 첨부 된 속성을 사용하여 해결책을 찾았습니다. 문제가 해결되지 않았지만 문제가 발생한 DataGrid 선택 문제를 해결하는 데 도움이됩니다.

다음은 추가 된 특성 및 핸들러 메소드

public static readonly DependencyProperty ComboBoxSelectionChangedProperty = DependencyProperty.RegisterAttached("ComboBoxSelectionChangedCommand", 
                               typeof(ICommand), 
                               typeof(SpDataGrid), 
                               new PropertyMetadata(new PropertyChangedCallback(AttachOrRemoveDataGridEvent))); 


public static void AttachOrRemoveDataGridEvent(DependencyObject obj, DependencyPropertyChangedEventArgs args) 
{ 
    DataGrid dataGrid = obj as DataGrid; 
    if (dataGrid != null) 
    { 
     if (args.Property == ComboBoxSelectionChangedProperty) 
     { 
     dataGrid.SelectionChanged += OnComboBoxSelectionChanged; 
     } 
    } 
    else if (args.OldValue != null && args.NewValue == null) 
    { if (args.Property == ComboBoxSelectionChangedProperty) 
     { 
     dataGrid.SelectionChanged -= OnComboBoxSelectionChanged; 
     } 
} 
} 

private static void OnComboBoxSelectionChanged(object sender, SelectionChangedEventArgs args) 
{ 
    DependencyObject obj = sender as DependencyObject; 
    ICommand cmd = (ICommand)obj.GetValue(ComboBoxSelectionChangedProperty); 
    DataGrid grid = sender as DataGrid; 

    if (args.OriginalSource is ComboBox) 
    { 
    if (grid.CurrentCell.Item != DependencyProperty.UnsetValue) 
    { 
     //grid.CommitEdit(DataGridEditingUnit.Row, true); 
     ExecuteCommand(cmd, grid.CurrentCell.Item); 
    } 
    } 
} 

SpDataGrid 난 데이터 그리드에서 상속 사용자 제어이다.

generic 스타일에 resourcedictionary를 사용하기 때문에 generic.xaml의 스타일을 아래에 추가했습니다 (확실히 DataGrid 내부에 추가 할 수 있음).

<Style TargetType="{x:Type Custom:SpDataGrid}"> 
    <Setter Property="Custom:SpDataGrid.ComboBoxSelectionChangedCommand" Value="{Binding ComboBoxSelectionChanged}"/> 
</Style> 

ComboBoxSelectionChanged는 내 viewmodel의 명령입니다. OnComboBoxSelectionChanged 필자는 내 경우 값이 이미 업데이트되었으므로 커밋 된 항목에 주석을 달았습니다.

명확하지 않은 점이나 질문이 있으면 알려주십시오. 희망이 도움이됩니다.