WPF 2 ComboBox绑定问题

发布于 2024-07-14 06:40:31 字数 1365 浏览 12 评论 0原文

我有以下问题:
有一个类有几个字符串属性
有这样的类实体的集合,

该集合显示在某些窗口左侧的树中,详细信息显示在右侧。 我详细地将所选节点的字符串属性绑定到组合框。
第一个组合框始终具有相同的 ItemsSource,但第二个 ItemsSource 取决于第一个组合的 SelectedItem...

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

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

挽手叙旧 2024-07-21 06:40:31

我刚刚找到了答案。
绑定按照声明的顺序进行通知,WPF 不会分析绑定的依赖关系:)
因此,交换 ComboBox 的声明可以解决问题...在这种情况下这是可以接受的,因为我将这些 ComboBox 放入 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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文