简体   繁体   中英

How do i prevent a two-way binding from changing my property after ive programatically set it

I have a piece of code that makes a db trip and pulls a bunch of records. Im using this method as a Get as well as a Refresh .

When itemToSelectAfterLoading is null , we select the first item (Get/Fresh load)

When itemToSelectAfterLoading is NOT null , we reselect that item after doing a reload.

    private async Task LoadGroupsAsync(RTG_Lookup2_Group itemToSelectAfterLoading)
    {
        _ea.GetEvent<ShowActivityIndicatorEvent>().Publish("Loading Groups...");

        ListOfLookupGroups = await _repository.GetGroupsAsync().ConfigureAwait(false);
        RaisePropertyChanged(nameof(ListOfLookupGroups));

        var id = itemToSelectAfterLoading?.Id;
        SelectedLookupGroup = id == null ? ListOfLookupGroups.FirstOrDefault() : ListOfLookupGroups.FirstOrDefault(f => f.Id == id);

        //RebuildAdBuilderAndWhatIfLists.Execute(null);
        _ea.GetEvent<HideActivityIndicatorEvent>().Publish();
    }

On the xaml side of things, SelectedLookupGroup is bound to the SelectedItem property of a combobox with a TwoWay binding and IsSynchronizedWithCurrentItem="True"

        <telerik:RadComboBox x:Name="GroupList"
                                 Grid.Column="1"
                                 ItemsSource="{Binding ListOfLookupGroups}"
                                 SelectedItem="{Binding SelectedLookupGroup, Mode=OneWay}"
                                 DisplayMemberPath="Name"
                                 IsSynchronizedWithCurrentItem="True"
                                 IsEditable="True"
                                 IsReadOnly="True"
                                 Margin="5"
                                 Padding="5" />

Everything works fine as expected when doing a fresh load, but im having issues with the reload where i set SelectedLookupGroup .

With a breakpoint on my SelectedLookupGroup setter, it gets hit twice while the method is ran once.

The first time the setter is hit, the right item is being passed and the second time, the first item of my collection is being passed.

On the first hit, my callstack shows the calling method and the setter.

在此处输入图片说明

On the second hit, i only see the setter, not whats calling it.

在此处输入图片说明

If I change this property to a OneWay binding, my refresh works as expected, but i lose UI interaction. So i think the UI is changing this property AFTER i set it with SelectedLookupGroup = itemToSelectAfterLoading ?? ListOfLookupGroups.FirstOrDefault(); SelectedLookupGroup = itemToSelectAfterLoading ?? ListOfLookupGroups.FirstOrDefault(); in my LoadGroupsAsync method

I figured i need to change the binding type when im doing the (re)load, but im not sure how to do that.

E: I removed all the async code (no more await/async) and everything works as expected. Not a good solution but its good enough for now...

This happens because you had set IsSynchronizedWithCurrentItem="True" .

Prior to Q2 2010 version, the current item was synchronized with the selected item. As a result, the first row of the GridView was selected initially. To prevent this, you would simply need to set the IsSynchronizedWithCurrentItem property of RadGridView to False . In Q2 2010 version, the IsSynchronizedWithCurrentItem is null by default. In this case, SelectedItem is synchronized with the CurrentItem only if CollectionView is used as ItemsSource .

Ref. https://docs.telerik.com/devtools/wpf/controls/radgridview/selection/selecteditem-currentitem

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