【问题标题】:How to get object bound to DataGridRow in Xaml?如何在 Xaml 中将对象绑定到 DataGridRow?
【发布时间】:2013-06-08 21:40:40
【问题描述】:

DataGrid 有 ItemsSource 作为 ObservableCollection。 ParticipantViewModel 具有名为 ExpenseType、Currency 等的属性。对于某些特定的费用类型,参与者可以更改货币,这可以从 ParticipantViewModel 的另一个名为 IsCurrencyEnable 的属性中确定。我将 IsCurrencyEnable 与 ComboBox IsEnabled 属性绑定。

但它在 MainViewModel 中搜索 IsCurrencyEnable 而不是 ParticipantViewModel。

如何绑定 IsEnabled 属性以使其从 ParticipantViewModel 获取值?

谢谢。

【问题讨论】:

  • 贴出相关代码和XAML。

标签: wpf data-binding mvvm viewmodel wpfdatagrid


【解决方案1】:

这很可能是数据上下文问题。您需要绑定以下内容

{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ControlTypeThatBindsToParticipantViewModel}},Path=DataContext.IsCurrencyEnable}

ControlTypeThatBindsToParticipantViewModel 这是将其数据上下文绑定到 ParticipantViewModel 的控件类型,可以是窗口、用户控件等。

希望对你有帮助

【讨论】:

  • 您好,感谢您的回复。 DataGrid ItemSource 的类型为 ObservableCollection(MainViewModel 的属性)。 DataGrid 的每一行都显示了 ParticipantViewModel 属性的数据绑定,因此它是绑定到 ParticipantViewModel 的 DataGridRow 控件。但是当我使用 {Bind IsCurrencyEnable} 时,它会在 MainViewModel 中搜索 IsCurrencyEnable 属性。
  • 您确实需要像 HighCore 建议的那样发布 xaml 代码,否则我们无能为力。你试过我的解决方案了吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-15
  • 2012-02-06
  • 2011-02-28
  • 1970-01-01
相关资源
最近更新 更多