WPF 2 ComboBox binding problem

对着背影说爱祢 提交于 2019-12-14 04:21:28

问题


I have the following problem:
there is a class with a couple of string properties
there is a collection of such class entities

That collection is shown in tree on the left of some windows and details shown on the right. I'm binding string properties of selected node to comboboxes in details.
First combobox always have the same ItemsSource but the second one ItemsSource depends on SelectedItem of the first combo...

<ComboBox 
  Grid.Column="1" 
  SelectedIndex="0"  
  x:Name="cbClass" 
  Style="{DynamicResource ComboBoxValidationError}" 
  SelectedValue="{Binding Path=Description.Node.ClassName, ElementName=userControl, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"  
  ItemsSource="{Binding Source={StaticResource classesProvider}}" 
  Width="Auto" 
  Height="Auto"  
  DisplayMemberPath="Description" 
  SelectedValuePath="FQN" />

<ComboBox 
  Grid.Column="1" 
  SelectedIndex="0" 
  Grid.Row="1"  
  x:Name="cbMethod" 
  SelectedValue="{Binding Path=Description.Node.MethodName, ElementName=userControl, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged,diag:PresentationTraceSources.TraceLevel=High}" 
  ItemsSource="{Binding Path=SelectedItem.Methods, ElementName=cbClass, Mode=Default,diag:PresentationTraceSources.TraceLevel=High}" 
  Style="{DynamicResource ComboBoxValidationError}" 
  Width="Auto" 
  Height="Auto" 
  SelectedValuePath="Name" 
  DisplayMemberPath="Description"  />

Now when i create new node in the tree, both string properties have null reference. And when first combo changes its SelectedItem for the NEW node, second ComboBox binds null to the string value of the OLD node, which were selected before creating new node in the tree... I wonder what should i do in this case?


回答1:


I've just found an answer.
Binding are notified in the order of their declaration, WPF is not going to analyse dependencies of bindings :) So swapping declarations of ComboBoxes solves the problem... It's acceptable in this scenario because I place these ComboBoxes in Grid manually setting their Grid.Row and Grid.Column... Though solution is not very pleasing, it works!



来源:https://stackoverflow.com/questions/606343/wpf-2-combobox-binding-problem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!