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