相关疑难解决方法(0)

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

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>
Run Code Online (Sandbox Code Playgroud)

随着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
        { …
Run Code Online (Sandbox Code Playgroud)

c# wpf wpfdatagrid

5
推荐指数
2
解决办法
1812
查看次数

DataGrid - 更改编辑行为

我有一个带有一些复杂行为的ChildViewModels的ObservableCollection.

当我去编辑一行时 - DataGrid进入'编辑模式' - 这有效地禁用了当前单元格之外的UI通知,直到提交了行 - 这是预期的行为,更重要的是它可以被更改吗?

例:

public class ViewModel
{
    public ViewModel()
    {
        Childs = new ObservableCollection<ChildViewModel> {new ChildViewModel()};
    }
    public ObservableCollection<ChildViewModel> Childs { get; private set; }
}
public class ChildViewModel : INotifyPropertyChanged
{
    private string _firstProperty;
    public string FirstProperty
    {
        get { return _firstProperty; }
        set
        {
            _firstProperty = value;
            _secondProperty = value;
            OnPropetyChanged("FirstProperty");
            OnPropetyChanged("SecondProperty");
        }
    }

    private string _secondProperty;
    public string SecondProperty
    {
        get { return _secondProperty; }
        set
        {
            _secondProperty = value;
            OnPropetyChanged("SecondProperty"); …
Run Code Online (Sandbox Code Playgroud)

c# wpf datagrid mvvm

1
推荐指数
2
解决办法
5270
查看次数

标签 统计

c# ×2

wpf ×2

datagrid ×1

mvvm ×1

wpfdatagrid ×1