我有一段代码可以进行数据库访问并提取一堆记录。我将此方法用作 Get
以及 Refresh
。
当itemToSelectAfterLoading
为null
时,我们选择第一项(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 。
第二次击球时,我只看到二传手,看不到它的名字。
如果我将此属性更改为 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 toFalse
. In Q2 2010 version, the IsSynchronizedWithCurrentItem is null by default. In this case,SelectedItem
is synchronized with theCurrentItem
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/