c# - WPF XAML 组合框在更新 ItemsSource 时与实际值同步
问题描述
我在保持组合框与实际选定值同步时遇到问题。我有一个数据网格,我将选定的行值绑定到一个对象。所选对象具有值为“test2”的属性代码。
我的组合框有一个选项“test1”和“test2”和“test3”
当我选择此对象时,组合框会更新为正确的值“test2”,但在更新 ItemsSource 时,由于我使用了 isSynchronizedWithCurrentItem = true,它会自动设置组合框中的第一个替代项。我的应用程序每 10 秒轮询一次新项目并更新/刷新 itemssource,我的问题是如何使组合框与实际选择的组合框替代项/项目同步?
就像我选择“test3”并且 itemssource 更新一样,组合框仍将显示“test3”而不是“test1”作为第一个默认值。或者更确切地说,如果我没有选择另一个值,它仍然会与所选对象“test”的当前值保持不变?
这是我的 XAML 代码: 我绑定到的对象是公共 UserObject 对象。
<TextBlock Grid.Column="1" Grid.Row="0" Text="Name:" VerticalAlignment="Center" />
<telerik:RadComboBox Grid.Column="2" Grid.Row="0" Text="{Binding Object.Code, Mode=TwoWay}" Margin="4 4 0 4" FontSize="13" IsReadOnly="True" ItemsSource="{Binding Objects}"
DisplayMemberPath="Code" SelectedValue = "{Binding Object.Code}" SelectedValuePath="Code"
SelectedItem="{Binding SelectedCode}" IsSynchronizedWithCurrentItem="True" />
我怎样才能解决这个问题?
解决方案
你可以试试这个:
private void RefreshFunc()
{
string objectCode = Object.Code;
//update/refresh the itemssource
ToRefreshItemsSource();
Object.Code = objectCode;
}
推荐阅读
- xcode - 当我将反应原生 iOS 应用程序上传到 App Store Connect 时,我得到了空版本
- swift - Swift:Twilio 发送短信,需要“收件人”电话号码
- google-bigquery - Airflow 如何将 API 请求结果写入 BigQuery 表或 GCS 文件
- c - C - 打印 ref 传递的成员链表
- c# - 如何将 C# 类对象转换为 json?
- python - 使用 Scrapy 和 Splash 在动态 JavaScript 网页中抓取图像
- python-3.x - 与其他特定机器人在公会中时,Discord 机器人功能停止工作
- postgresql - 将源数据库作为命令行参数传入
- ios - `QLPreviewItem` 是否仅适用于本地文件?
- flutter - SoundPlayerUI() 的 Flutter_Sound 问题