【问题标题】:How to bind to two properties in different sources?如何绑定到不同来源的两个属性?
【发布时间】:2016-08-28 15:26:13
【问题描述】:

我想要一个组合框,它的 ItemsSource 来自 posteViewSource,它的 SelectedItem 绑定到链接到 superviseurViewSource 的表的 idPoste 字段。

<Window.Resources>
    <local:StagesBDDataSet x:Key="stagesBDDataSet"/>
    <CollectionViewSource x:Key="superviseurViewSource" Source="{Binding Superviseur, Source={StaticResource stagesBDDataSet}}"/>
    <CollectionViewSource x:Key="posteViewSource" Source="{Binding Poste, Source={StaticResource stagesBDDataSet}}"/>
</Window.Resources>

这是我的组合框定义:

        <ComboBox x:Name="idPosteComboBox" DataContext="{StaticResource posteViewSource}" ItemsSource="{Binding}" Height="23" Width="120" DisplayMemberPath="idPoste" />

组合框位于 DataContext 设置为 {StaticResource superviseurViewSource} 的网格中。与项目的绑定工作完美,但我不知道是否可以为 SelectedItem 属性提供另一个 DataContext。任何提示将不胜感激,谢谢。

【问题讨论】:

  • 没有意义,将SelectedItem 绑定到项目的idPoste -> 是的,将其绑定到CollectionViewSource -> 否,绑定到此集合中的哪个项目?
  • 那也是 idPoste。组合框填充了 Poste 表的行(链接到上面提到的 collectionviewsource),效果很好。现在我想要的是将该组合框中的选定项与Supervisor 表的collectionviewsource 的当前行中的字段idPoste 绑定。解释它对我来说有点挑战性,我希望这会有所帮助。
  • 那么您必须跟踪此表中当前选定的项目,您可能正在使用数据网格或其他东西:stackoverflow.com/a/13755582/361899。一旦你有了它,你就可以绑定到控件中其他地方的属性,比如&lt;ComboBox DataContext="{StaticResource resource1}" Content="{Binding TextInResource1}" Tooltip="{Binding ElementName=resource2, Path=Something}" /&gt;
  • 谢谢!效果很好。

标签: c# wpf data-binding


【解决方案1】:

你可以有一个 ItemSource 的绑定和另一个 SelectedItem 的绑定,所以你可以这样写:

<ComboBox x:Name="idPosteComboBox"
   DataContext="{StaticResource posteViewSource}" ItemsSource="{Binding}"
   SelectedItem="{Binding ---another binding---}"
   IsSynchronizedWithCurrentItem="True"
   Height="23" Width="120" DisplayMemberPath="idPoste" />

【讨论】:

  • 感谢您的宝贵时间。我试过了,但没有运气。 SelectedItem 的绑定可以吗? SelectedItem="{Binding Source=superviseurViewSource, Path=idPoste, Mode=TwoWay}"
猜你喜欢
  • 1970-01-01
  • 2019-04-30
  • 1970-01-01
  • 1970-01-01
  • 2018-08-08
  • 2012-11-11
  • 1970-01-01
  • 2018-12-09
  • 2021-02-06
相关资源
最近更新 更多