ComboBox的SelectedItem绑定不更新。

10

我有些困惑:

这个可以工作:

<StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
            <Label Content="Rol" />
            <ComboBox ItemTemplate="{StaticResource listRollen}"
                      Height="23" Width="150"
                      SelectedItem="{Binding Path=SelectedRol, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
                      ItemsSource="{Binding Path=allRollen, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
        </StackPanel>

所以SelectedRol的属性为:

public TblRollen SelectedRol
    {
        get { return _selectedRol; }
        set
        {
            if (_selectedRol != value)
            {
                _selectedRol = value;
                OnPropertyChanged("SelectedRol");
            }
        }
    }

但是这样不起作用:

<StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
            <Label Content="Soort" />
            <ComboBox ItemTemplate="{StaticResource listSoorten}"
                      Height="23" Width="150"
                      ItemsSource="{Binding Path=allSoorten}"
                      SelectedItem="{Binding Path=SelectedProduct, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
        </StackPanel>

具有以下属性 SelectedProduct:

public TblProduktSoorten SelectedProduct
    {
        get { return _selectedPSoort; }
        set
        {
            if (_selectedPSoort != value)
            {
                _selectedPSoort = value;
                OnPropertyChanged("SelectedProduct");
            }
        }
    }

在我的代码中,我设置了 SelectedProduct = p.TblProduktSoorten,在调试时,我发现属性被正确设置了...


8
雪熊厌倦了“不起作用”的模式。拯救雪熊!告诉他你的情况中有什么不起作用! - Snowbear
抱歉,操作太快了。 当属性通过listbox中的selectionChanged事件获取新值时,SelectedRol的combobox正确显示属性。 然而,几乎完全相同的代码,SelectedPSoort的combobox从不显示任何内容。ItemsSource有效,SelectedItem被填充但不在combobox中显示。如果这有意义的话... - olvr_vrmr
你确定你的ItemsSource包含TblProduktSoorten类型的对象吗?因为如果不是,你在SelectedItem绑定上将不会得到任何错误提示。 - Dummy01
我的 ItemsSourceObservableCollection<TblProduktSoorten> allSoorten = new ObservableCollection<TblProduktSoorten>(pRep.Service.GetAll<TblProduktSoorten>()) - olvr_vrmr
绑定只能针对属性而非字段进行操作。(https://dev59.com/k3RA5IYBdhLWcg3wvgob) - CodeNaked
显示剩余2条评论
9个回答

15

1
同样适用于其他控件,如“RadioButton”。不确定确切的标准或为什么需要这样做。可能与可视树层次结构有关。 - Simon_Weaver

7
尝试使用值路径而不是选定项,请查看代码示例。
<ComboBox Name="projectcomboBox" ItemsSource="{Binding Path=Projects}" IsSynchronizedWithCurrentItem="True" DisplayMemberPath="FullName"
          SelectedValuePath="Name"  SelectedIndex="0"  Grid.Row="1" Visibility="Visible" Canvas.Left="10" Canvas.Top="24" Margin="11,6,13,10">
</ComboBox>

绑定属性是指在IT技术中用于将数据与UI元素进行关联的属性。
public ObservableCollection<Project> Projects
{
    get { return projects; }
    set
    {
        projects = value;
        RaisePropertyChanged("Projects");
    }
}

9
"IsSynchronizedWithCurrentItem="True" 帮了我。谢谢。" - RDV
@RDV 哦,这也解决了我的问题!对我来说,更新起作用了,但只在每隔一个选择时才有效!我不知道这种奇怪的情况是什么原因。但是添加 IsSynchronizedWithCurrentItem="True",解决了这个问题,现在每次更新都可以正常绑定了! - Nicolas
@RDV 如果您能看一下我提供的代码,您会发现已经设置了 IsSynchronizedWithCurrentItem="True"。 - Sergey K
是的,那是我代码中唯一缺少的东西,我感谢你为此。 - RDV

6
这可能与一个事实有关,即 显然属性的顺序很重要,在你的第二种情况下,ItemsSourceSelectedItem 的声明被交换了。

1
我必须说,对于经过适当测试的组件来说,我认为这完全是无稽之谈。虽然在技术上可能是可行的(我仍然不确定),但您永远不应该为Microsoft控件而担心这个问题。 - Simon_Weaver

6

我的问题是由于我自己疲倦的大脑引起的。相同的症状,也许会让你看到自己的问题。

设置SelectedItem时必须提供列表中的一个项目!(显然)通常这会自然发生,但我有一个例子,我从另一个服务(相同的对象类型)中获取了一个 "角色",并试图设置它,并期望组合框发生变化! ;(

改为-

Roles = roles;
CurrentRole = role;

请务必记住执行以下操作 -

Roles = roles;
CurrentRole = roles.FirstOrDefault(e=> e.ID == role.ID); //(System.Linq)

1
犯了同样的错误!一开始就有这种感觉,可能是根本原因,但还是在网上搜索了一个小时,直到看到你的答案,哈哈。 - drkthng
刚刚犯了同样的错误,很高兴找到了你的帖子。 - Athanasios Emmanouilidis

3
如果在属性改变事件处理程序中更改SelectedProduct属性时,你需要异步地设置该属性。
private void ViewModel_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
    if (e.PropertyName == "SelectedProduct")
        App.Current.Dispatcher.InvokeAsync(() => SelectedProduct = somevalue);
}

2
我认为这个问题是由于ItemSource和SelectedItem的类型不匹配引起的。
例如,如果ItemSource绑定到一个int列表,而SelectedItem绑定到一个字符串。如果你将SelectedItem设置为null或空字符串,组合框就无法知道选中了哪个项目。所以组合框将显示空白。

2

我不知道你是否已经解决了这个问题,但今天我也遇到了同样的问题。将selecteditems的集合类型更改为ObservableCollection后,问题得以解决。


我已经尝试了所有其他答案,但都无济于事。这个答案对我起作用了。简单明了。 - rr789

0

这可能有点老了,但我还没有看到能解决我的问题的技巧;我不得不在ComboBox中的SelectedItem中添加NotifyOnSourceupdate=true


0

在DataGrid中,使用SelectedItem让我困惑了一段时间。一切都很好,但是我正在反序列化应用程序状态,加载项目并且还有一个选定的项目。集合已经存在,但是选定的项目实际上直到我使用Text="{Binding Path=Score.SelectedResult.Offset}"才可见。

<DataGridTemplateColumn.CellTemplate>
    <DataTemplate>
        <ComboBox ToolTip="Score offset results" 
          ItemsSource="{Binding Score.SearchResults,UpdateSourceTrigger=PropertyChanged}"                                                   
          SelectedItem="{Binding Score.SelectedResult, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
          Text="{Binding Path=Score.SelectedResult.Offset}"
          SelectedValuePath="Offset"
          DisplayMemberPath="Offset"/>
    </DataTemplate>
</DataGridTemplateColumn.CellTemplate>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接