비슷한 문제가 있었지만 방금 첨부 된 속성을 사용하여 해결책을 찾았습니다. 문제가 해결되지 않았지만 문제가 발생한 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 필자는 내 경우 값이 이미 업데이트되었으므로 커밋 된 항목에 주석을 달았습니다.
명확하지 않은 점이나 질문이 있으면 알려주십시오. 희망이 도움이됩니다.