[英]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.