WPF 2组合框绑定问题 [英] WPF 2 ComboBox binding problem

查看:91
本文介绍了WPF 2组合框绑定问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下问题:

有一个类有几个字符串属性

有一个这样的类实体的集合

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

该集合显示在一些窗口的左侧的树中,右侧显示的细节。我将详细地将选定节点的字符串属性绑定到组合框。

第一个组合框始终具有相同的ItemsSource,第二个组合框依赖于第一个组合的SelectedItem ...

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"  />

现在当我在树中创建新节点时,两个字符串属性都有空引用。当第一个组合更改其新节点的SelectedItem时,第二个ComboBox将null绑定到OLD节点的字符串值,这是在树中创建新节点之前选择的...我不知道在这种情况下应该怎么做?

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?

推荐答案

我刚刚找到了答案。

绑定会按照声明的顺序通知, WPF不会分析绑定的依赖:)
所以交换ComboBoxes的声明解决了这个问题...在这种情况下是可以接受的,因为我把这些ComboBoxes在Grid中手动设置他们的Grid.Row和Grid.Column .. 。
虽然解决方案不是很愉快,但它工作!

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!

这篇关于WPF 2组合框绑定问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆