ComboBox SelectedItem绑定未更新

olv*_*rmr 1 c# wpf xaml binding combobox

我有点困惑:这有效:

<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>
Run Code Online (Sandbox Code Playgroud)

和SelectedRol的属性是:

public TblRollen SelectedRol
    {
        get { return _selectedRol; }
        set
        {
            if (_selectedRol != value)
            {
                _selectedRol = value;
                OnPropertyChanged("SelectedRol");
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

但这不起作用:

<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>
Run Code Online (Sandbox Code Playgroud)

具有以下属性SelectedProduct:

public TblProduktSoorten SelectedProduct
    {
        get { return _selectedPSoort; }
        set
        {
            if (_selectedPSoort != value)
            {
                _selectedPSoort = value;
                OnPropertyChanged("SelectedProduct");
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

在我设置的代码的某处,SelectedProduct = p.TblProduktSoorten在调试时,我看到属性设置正确...

Ser*_*hei 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>
Run Code Online (Sandbox Code Playgroud)

绑定属性是

public ObservableCollection<Project> Projects
{
    get { return projects; }
    set
    {
        projects = value;
        RaisePropertyChanged("Projects");
    }
}
Run Code Online (Sandbox Code Playgroud)

  • IsSynchronizedWithCurrentItem="True" 帮助了我。谢谢 (9认同)

H.B*_*.B. 6

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


Sim*_*ver 6

DataGrid中的Combobox?

如果组合框在一个DataGrid你必须添加:

Mode=TwoWay, UpdateSourceTrigger=PropertyChanged
Run Code Online (Sandbox Code Playgroud)

请参阅:https://stackoverflow.com/a/5669426/16940


小智 6

我的问题是由我自己疲惫的大脑引起的。同样的症状,也许它会让你发现自己的问题。

设置SelectedItem必须在List中指定一个项目!(duhh)通常这种情况会自然发生,但我有一个案例,我从另一个服务(相同的对象类型)获得了“角色”,并尝试设置它并期望组合框发生变化!;(

代替 -

Roles = roles;
CurrentRole = role;
Run Code Online (Sandbox Code Playgroud)

记得这样做 -

Roles = roles;
CurrentRole = roles.FirstOrDefault(e=> e.ID == role.ID); //(System.Linq)
Run Code Online (Sandbox Code Playgroud)