简体   繁体   English

ComboBox SelectedItem 绑定不更新

[英]ComboBox SelectedItem binding not updating

I'm a bit puzzled: this works:我有点困惑:这有效:

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

and the property for SelectedRol is: SelectedRol 的属性是:

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

But this doesn't work:但这不起作用:

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

with following property SelectedProduct:具有以下属性 SelectedProduct:

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

somewhere in my code I set SelectedProduct = p.TblProduktSoorten and while debugging, I see the property gets set correctly...在我的代码中的某个地方,我设置了SelectedProduct = p.TblProduktSoorten并且在调试时,我看到该属性设置正确......

Combobox inside a DataGrid?数据网格中的 Combobox?

If the combobox is in a DataGrid you must add this:如果 combobox 在DataGrid中,则必须添加以下内容:

Mode=TwoWay, UpdateSourceTrigger=PropertyChanged

See this: https://stackoverflow.com/a/5669426/16940看到这个: https://stackoverflow.com/a/5669426/16940

This might be related to the fact that apparently attribute order does matter , in your second case the ItemsSource and SelectedItem declarations are swapped.这可能与显然属性 order 确实很重要的事实有关,在第二种情况下, ItemsSourceSelectedItem声明被交换了。

Try to use not selected item but value path look at the code sample尝试使用未选择的项目但值路径查看代码示例

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

the binding property is绑定属性是

public ObservableCollection<Project> Projects
{
    get { return projects; }
    set
    {
        projects = value;
        RaisePropertyChanged("Projects");
    }
}

I don't know if you fixed it yet, but I encountered the same issue today.不知道你有没有解决,今天我遇到了同样的问题。 It was fixed by making sure the collection for selecteditems was an ObservableCollection.通过确保 selecteditems 的集合是 ObservableCollection 来修复它。

If you set the SelectedProduct property when SelectedProduct is changed in the property changed event handler, you need to set this property asynchronously.如果在属性更改事件处理程序中更改 SelectedProduct 时设置 SelectedProduct 属性,则需要异步设置此属性。

private void ViewModel_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
    if (e.PropertyName == "SelectedProduct")
        App.Current.Dispatcher.InvokeAsync(() => SelectedProduct = somevalue);
}

My problem was caused by my own tired brain.我的问题是我自己疲惫的大脑造成的。 Same symptom, maybe it will kick you into seeing your problem.同样的症状,也许它会让你看到你的问题。

Setting the SelectedItem must be given an item in the List;!设置 SelectedItem 必须在 List 中给出一个项目;! (duhh) Normally this happens naturally but I had a case I got a "Role" from another service (Same object type) and was trying to set it and expecting the combobox to change! (duhh)通常这是自然发生的,但我有一个案例,我从另一个服务(相同的 object 类型)获得了一个“角色”,并试图设置它并期望 combobox 改变! ;( ;(

instead of -代替 -

Roles = roles;
CurrentRole = role;

remember to do this -记得这样做 -

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

I think this problem is caused by the type of ItemSource and SelectedItem is mitchmatched .我认为这个问题是由 ItemSource 的类型引起的,并且 SelectedItem 是 mitchmatched

For example, if the ItemSource is binded to a List of int and the SelectedItem is binded to a string .例如,如果 ItemSource 绑定到int 的 List并且 SelectedItem 绑定到string If you set selected item to null or empty string, the combobox cannot know what item is selected.如果将选中项设置为 null 或空字符串,则 combobox 无法知道选中了哪个项目。 So the combobox will show nothing.所以 combobox 什么都不会显示。

This might be old but I have not seen the trick that did it for me;这可能是旧的,但我还没有看到为我做的技巧; I had to add NotifyOnSourceupdate=true to my SelectedItem in the ComboBox我必须将NotifyOnSourceupdate=true添加到 ComboBox 中的SelectedItem

This had me stumped for a while inside a DataGrid, using SelectedItem.这让我在 DataGrid 中使用 SelectedItem 难住了一段时间。 Everything was fine but I am deserializing the app state which loads the items and also has a selected item.一切都很好,但我正在反序列化加载项目并且还有一个选定项目的应用程序状态。 The collection was there but the selected isn't actually visible until I used the Text="{Binding Path=Score.SelectedResult.Offset}"集合在那里,但在我使用 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM