【问题标题】:WPF two way binding with explicit source updating is not working带有显式源更新的 WPF 双向绑定不起作用
【发布时间】:2014-12-07 17:10:20
【问题描述】:

我绑定了 ObservableCollection - DataGrid(模式 - TwoWay),但我希望自己通过 UpdateSource() 调用更新集合并禁用自动源更新。我将绑定设置为

ItemsSource="{Binding Path=Bezier.BezierPoints, Mode=TwoWay, UpdateSourceTrigger=Explicit}"

但我的收藏仍会自动更新。我的代码示例如下。我究竟做错了什么? 我的 XAML:

<DataGrid Name="BezierPointsDataGrid" Margin="5" AutoGenerateColumns="False"
                  Grid.Column="0" Grid.Row="0" Background="White"
                  ItemsSource="{Binding Path=Bezier.BezierPoints, Mode=TwoWay, UpdateSourceTrigger=Explicit}">
            <DataGrid.Columns>
                <DataGridTextColumn Header="X" Binding="{Binding Path=X}" Width="1*"/>
                <DataGridTextColumn Header="Y" Binding="{Binding Path=Y}" Width="1*"/>
            </DataGrid.Columns>
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="SelectionChanged">
                    <i:InvokeCommandAction Command="{Binding Path=UpdateBezierPointsCommand}" CommandParameter="{Binding ElementName=BezierPointsDataGrid}"/>
                </i:EventTrigger>
            </i:Interaction.Triggers>
        </DataGrid>

我的视图模型:

class BezierCurveViewModel : INotifyPropertyChanged
{
    #region Bezier curve model

    private BezierCurveModel _bezier;

    public BezierCurveModel Bezier
    {
        get { return _bezier; }
        set
        {
            if (_bezier == value)
                return;
            _bezier = value;
            OnPropertyChanged("Bezier");
        }
    }

    #endregion

    #region Commands

    public ICommand UpdateBezierPointsCommand { set; get; }

    #endregion 

    #region Constructor

    public BezierCurveViewModel()
    {
        UpdateBezierPointsCommand = new Command(a => ((DataGrid)a).GetBindingExpression(DataGrid.ItemsSourceProperty).UpdateSource());
        Bezier = new BezierCurveModel();
    }

    #endregion

    #region INotifyPropertyChanged implementation

    public event PropertyChangedEventHandler PropertyChanged;

    [NotifyPropertyChangedInvocator]
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }

    #endregion
}

我的模特:

public ObservableCollection<DPoint> BezierPoints { private set; get; }

编辑:我将 ObservableCollection 更改为 DataTable 以实现预期的行为。但我仍然有兴趣解决这个问题,因为我想了解为什么在编辑表后任何绑定到可观察集合都会更新源(阅读我对 Andrew 帖子的评论)。

【问题讨论】:

    标签: c# wpf data-binding


    【解决方案1】:

    在这里,您已将视图设置为显式更新 BezierPoints 属性,因为您正在绑定 ItemsSource。

    我假设您真正想要的是对各个点的属性使用显式更新触发器。为此,您需要将 DataGridTextColum 绑定更改为 UpdateSourceTrigger=Explicit。

    附带说明一下,您似乎根本不可能从 View 更新 BezierPoints 集合,因为该属性有一个私有 setter。

    【讨论】:

    • 最奇怪的是,当我在{Binding Path=Bezier.BezierPoints, Mode=OneTime} 上更改数据网格的绑定并允许自动列在表更新时我的集合仍在更新(我想这不是一次性的绑定正确行为)。关于私有 setter - 我将其设为私有,因为我不希望在 viewmodel 代码中像 BezierPoints = new .... 那样分配我的集合,我只需要添加、删除、更新操作。
    • 更新是什么意思?什么具体的对象正在更新?
    猜你喜欢
    • 2014-04-10
    • 2011-10-14
    • 1970-01-01
    • 1970-01-01
    • 2019-11-13
    • 1970-01-01
    • 1970-01-01
    • 2011-09-17
    • 2012-02-14
    相关资源
    最近更新 更多