Wpf DataGrid SelectedItem在单元格编辑后丢失绑定

时间:2013-01-10 15:07:51

标签: c# wpf wpfdatagrid

我有一个DataGrid绑定到一组项目(规则)。如果我在DataGrid中手动编辑其中一个项目,似乎网格上的SelectedItem绑定停止工作(控制器中的RuleListViewModelPropertyChanged永远不再被调用)。但是,只有当我实际更改单元格中项目的值时,否则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
        }
    }
}

我真的不知道问题是什么,但是因为我实际上必须更改值来体验这种行为(只在单元格中单击并且没有编辑任何工作正常)我猜它与之有关当我更改绑定到它的项的值时SelectedItem绑定中断?!

3 个答案:

答案 0 :(得分:2)

对于遇到此线程并且您正在使用ObservableCollection的任何人 - 检查是否重写GetHashCode()(我正在通过IEquatable进行相等性测试)。一旦对单元格进行更改,对SelectedItem和SelectedIndex的绑定就会中断。

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

  

&#34;当对象包含在依赖于其哈希码的集合中时,您可以确保可变对象的哈希码不会改变。&#34;

当然,我在可编辑(即可变)字段上得到了一个哈希...

答案 1 :(得分:0)

我认为这是因为datagrid使用了Bindingroups,因此当您在单元格中输入时,每个UI事件都会被旁路,直到您验证并离开该行。

阅读这篇文章可能有所帮助:WPF DataGrid source updating on cell changed

答案 2 :(得分:0)

事实证明,这是因为我忘了让我的数据模型继承自System.Waf.Applications.DataModel(也许我应该提到我正在使用WPF应用程序框架)。

我想这与未处理的PropertyChanged事件有关。

相关问题