【问题标题】:Wpf DataGrid SelectedItem loses binding after cell editWpf DataGrid SelectedItem 在单元格编辑后失去绑定
【发布时间】:2013-01-10 15:07:50
【问题描述】:

我有一个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当我更改绑定到它的项目的值时绑定中断?!

【问题讨论】:

    标签: c# wpf wpfdatagrid


    【解决方案1】:

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

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

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

    当然,我得到了可编辑(即可变)字段的哈希值...

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

      • 感谢您的意见,但事实并非如此。这是因为我的DataModel 没有继承自System.Waf.Applications.DataModel(也许我应该提到我正在使用 WPF 应用程序框架)
      【解决方案3】:

      原来是因为我忘记让我的数据模型从System.Waf.Applications.DataModel 继承(也许我应该提到我正在使用 WPF 应用程序框架)。

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-11-07
        • 1970-01-01
        • 2012-12-04
        • 2014-12-13
        • 1970-01-01
        • 1970-01-01
        • 2012-06-12
        • 2011-11-27
        相关资源
        最近更新 更多