简体   繁体   中英

WPF two way binding with explicit source updating is not working

I have binding ObservableCollection - DataGrid (mode - TwoWay), but i want update collection by myself with UpdateSource() call and disable automathic source updating. I set binding like

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

but my collection still updates automathically. My code samples are below. What am i doing wrong? My 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>

My ViewModel:

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
}

My model:

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

EDIT: I changed ObservableCollection To DataTable to achieve expected behaviour. But i am still interested in solving this problem because i want to understand why any binding to observable collection updates source after editing table (read my comment to Andrew's post).

Here, you have set up the view to update the BezierPoints property explicitly, since that is what you are binding the ItemsSource.

I will assume that what you actually want is to use an Explicit update trigger on the properties of the individual points. To do this, you need to change the DataGridTextColum binding to UpdateSourceTrigger=Explicit.

As a side note, it would seem impossible that you could ever update the BezierPoints collection from the View at all because the property has a private setter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM