首页 > 解决方案 > 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"  />

我怎样才能解决这个问题?

标签: c#wpfxamltelerik

解决方案


你可以试试这个:

private void RefreshFunc()
    {
        string objectCode = Object.Code;

        //update/refresh the itemssource
        ToRefreshItemsSource();

        Object.Code = objectCode;
    }

推荐阅读