c# - 在我以编程方式设置属性后,如何防止双向绑定(bind)更改我的属性

标签 c# wpf

我有一段代码可以进行数据库访问并提取一堆记录。我将此方法用作 Get 以及 Refresh

itemToSelectAfterLoadingnull时,我们选择第一项(Get/Fresh load)

itemToSelectAfterLoading 不是 null 时,我们会在重新加载后重新选择该项目。

    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();
    }

在 xaml 方面,SelectedLookupGroup 通过 TwoWay 绑定(bind)和 IsSynchronizedWithCurrentItem="True" 绑定(bind)到组合框的 SelectedItem 属性>

        <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" />

在执行新加载时一切正常,但我在设置 SelectedLookupGroup 时遇到问题。

在我的 SelectedLookupGroup setter 上有一个断点,当该方法运行一次时它会被命中两次。

第一次命中 setter 时,正确的项目被传递,第二次,我的收藏中的第一个项目被传递。

在第一次点击时,我的调用堆栈显示了调用方法和 setter 。

enter image description here

第二次击球时,我只看到二传手,看不到它的名字。

enter image description here

如果我将此属性更改为 OneWay 绑定(bind),我的刷新会按预期工作,但我会失去 UI 交互。所以我认为在我用 SelectedLookupGroup = itemToSelectAfterLoading 设置它之后,UI 正在更改这个属性? ListOfLookupGroups.FirstOrDefault(); 在我的 LoadGroupsAsync 方法中

我想我在执行(重新)加载时需要更改绑定(bind)类型,但我不确定该怎么做。

E:我删除了所有异步代码(不再有 await/async),一切都按预期工作。这不是一个好的解决方案,但现在已经足够了......

最佳答案

发生这种情况是因为您设置了 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.

引用。 https://docs.telerik.com/devtools/wpf/controls/radgridview/selection/selecteditem-currentitem

关于c# - 在我以编程方式设置属性后,如何防止双向绑定(bind)更改我的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50706978/

相关文章:

Wpf - 资源查找

c# - 是否需要实例化一个类以便您读取其属性之一?

c# - 如果实例表示泛型参数,Type.Namespace 何时返回 null

c# - 在新的 Azure Functions 项目中重复 'System.Reflection.AssemblyCompanyAttribute' 属性

wpf - 重置 TranslateZoomRotateBehavior? (WPF/混合行为)

wpf - 如何向已使用样式模板的窗口添加其他样式

c# - DLL 中的 HOOK C# 方法

c# - TPL 如何知道线程是否要求更多工作?

wpf - 如何删除样式中定义的 WPF 动画?

c# - 如何清除RichTextBox中的文本内容