2013-01-10 143 views
0

我有一個DataGrid綁定到項目集合(規則)。如果我在DataGrid中手動編輯這些項目之一,看起來好像網格上的SelectedItem綁定停止工作(控制器中的RuleListViewModelPropertyChanged永遠不會再被調用)。但只有當我實際改變單元格中的項目的值時,否則SelectedItem會繼續按照它應該的方式工作。Wpf DataGrid SelectedItem在單元格編輯後失去綁定

我已經剝去了一些不相關的東西的代碼,所以這基本上就是我所擁有的。首先,我有以下DataGrid

<DataGrid x:Name="RuleTable" Grid.Row="1" Grid.ColumnSpan="2" ItemsSource="{Binding Rules}" SelectedItem="{Binding SelectedRule, Mode=TwoWay}" 
       BorderThickness="0" > 
     <DataGrid.Columns> 
      <DataGridTextColumn Binding="{Binding TargetValue, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True, 
            ValidatesOnExceptions=True, NotifyOnValidationError=True}" 
           Header="{x:Static p:Resources.TargetValue}" Width="*" ElementStyle="{StaticResource TextCellElementStyle}" 
           EditingElementStyle="{StaticResource TextCellEditingStyle}"/> 
     </DataGrid.Columns> 
    </DataGrid> 

隨着ViewModel,看起來像這樣:

public class RuleListViewModel : ViewModel<IRuleListView> 
{ 
    private IEnumerable<Rule> rules; 
    private Rule selectedRule; 

    public RuleListViewModel(IRuleListView view) 
     : base(view) 
    { 
    } 

    public RuleListViewModel() : base(null) {} 

    public IEnumerable<Rule> Rules 
    { 
     get 
     { 
      return rules; 
     } 
     set 
     { 
      if (rules != value) 
      { 
       rules = value; 
       RaisePropertyChanged("Rules"); 
      } 
     } 
    } 

    public Rule SelectedRule 
    { 
     get 
     { 
      return selectedRule; 
     } 
     set 
     { 
      if (selectedRule != value) 
      { 
       selectedRule = value; 
       RaisePropertyChanged("SelectedRule"); 
      } 
     } 
    } 
} 

最後一個Controller,看起來像這樣:

public class RuleController : Controller 
{ 
    private readonly IShellService shellService; 
    private readonly RuleListViewModel ruleListViewModel; 

    private readonly DelegateCommand addRuleCommand; 
    private readonly DelegateCommand deleteRuleCommand; 

    [ImportingConstructor] 
    public RuleController(IShellService shellService, IEntityService entityService, RuleListViewModel ruleListViewModel) 
    { 
     this.shellService = shellService; 
     this.ruleListViewModel = ruleListViewModel; 
    } 

    public void Initialize() 
    { 
     AddWeakEventListener(ruleListViewModel, RuleListViewModelPropertyChanged); 
     shellService.RuleListView = ruleListViewModel.View; 

     List<Rule> rules = GetRules(); 
     ruleListViewModel.Rules = new ObservableCollection<Rule>(rules); 
    } 

    private void RuleListViewModelPropertyChanged(object sender, PropertyChangedEventArgs e) 
    { 
     if (e.PropertyName == "SelectedRule") 
     { 
      // This never gets called after edit 
     } 
    } 
} 

我也實在沒有想法是什麼問題,但因爲我實際上不得不改變價值體驗這種行爲(只有單擊單元格,而不是編輯任何工程f ine)我猜測它與SelectedItem綁定破解有關,當我改變綁定到它的項目的值時!

回答

0

因此,原來,這是因爲我忘了讓我的數據模型繼承System.Waf.Applications.DataModel(也許我應該提到,我使用WPF應用程序框架)。

我想這跟PropertyChanged事件沒有被處理。

0

我認爲這是因爲datagrid使用Bindingroups,所以當你進入單元格時,每一個UI事件都會被旁路,直到你驗證並離開該行。

閱讀這篇文章它可能有助於:WPF DataGrid source updating on cell changed

+0

謝謝您的輸入,但事實並非如此。這是因爲我的'DataModel'沒有從'System.Waf.Applications.DataModel'繼承(也許我應該提到我正在使用WPF應用程序框架) – Joel

1

對於任何遇到此線程並且您正在使用ObservableCollection的人 - 請檢查您是否重寫GetHashCode()(我正在通過IEquatable進行相等性測試)。只要對單元格進行更改,與SelectedItem和SelectedIndex的綁定就會中斷。

從MSDN(https://msdn.microsoft.com/en-us/library/system.object.gethashcode(v=vs.110).aspx):

「你可以確保在有關的對象包含一個依賴散列碼集合中的可變對象的哈希碼不會改變。」

當然,我得到的編輯(即可變)字段的哈希...