[英]ComboBox SelectedItem binding not updating
我有点困惑:这有效:
<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
并且在调试时,我看到该属性设置正确......
如果 combobox 在DataGrid
中,则必须添加以下内容:
Mode=TwoWay, UpdateSourceTrigger=PropertyChanged
这可能与显然属性 order 确实很重要的事实有关,在第二种情况下, ItemsSource
和SelectedItem
声明被交换了。
尝试使用未选择的项目但值路径查看代码示例
<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>
绑定属性是
public ObservableCollection<Project> Projects
{
get { return projects; }
set
{
projects = value;
RaisePropertyChanged("Projects");
}
}
不知道你有没有解决,今天我遇到了同样的问题。 通过确保 selecteditems 的集合是 ObservableCollection 来修复它。
如果在属性更改事件处理程序中更改 SelectedProduct 时设置 SelectedProduct 属性,则需要异步设置此属性。
private void ViewModel_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == "SelectedProduct")
App.Current.Dispatcher.InvokeAsync(() => SelectedProduct = somevalue);
}
我的问题是我自己疲惫的大脑造成的。 同样的症状,也许它会让你看到你的问题。
设置 SelectedItem 必须在 List 中给出一个项目;! (duhh)通常这是自然发生的,但我有一个案例,我从另一个服务(相同的 object 类型)获得了一个“角色”,并试图设置它并期望 combobox 改变! ;(
代替 -
Roles = roles;
CurrentRole = role;
记得这样做 -
Roles = roles;
CurrentRole = roles.FirstOrDefault(e=> e.ID == role.ID); //(System.Linq)
我认为这个问题是由 ItemSource 的类型引起的,并且 SelectedItem 是 mitchmatched 。
例如,如果 ItemSource 绑定到int 的 List并且 SelectedItem 绑定到string 。 如果将选中项设置为 null 或空字符串,则 combobox 无法知道选中了哪个项目。 所以 combobox 什么都不会显示。
这可能是旧的,但我还没有看到为我做的技巧; 我必须将NotifyOnSourceupdate=true
添加到 ComboBox 中的SelectedItem
这让我在 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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.