绑定到xaml中的CollectionViewSource

时间:2011-08-18 12:32:30

标签: wpf xaml data-binding

我有一个具有2个属性的ViewModel:

  • IsReadOnly
  • SomeCollectionViewSource

这是一个简单的工作视图示例:

<StackPanel DataContext="{Binding SomeCollectionViewSource}">
  <DatePicker SelectedDate="{Binding Path=Date}" IsEnabled="False" />      
</StackPanel>

现在我要绑定IsEnabled属性:

<StackPanel DataContext="{Binding}">
  <DatePicker SelectedDate="{Binding Path=?}" IsEnabled="{Binding IsReadOnly}" />      
</StackPanel>

在这个例子中,绑定应该如何? (我想我很简单) 我更喜欢简短的绑定,因为我有很多控件要绑定。

是否有更好/更简单的方法使一个CollectionViewSource上的所有控件都是只读的?

1 个答案:

答案 0 :(得分:1)

假设上述绑定针对当前项目,这应该是等效的:

{Binding SomeCollectionViewSource.View/Date}

另请参阅Binding.PathPropertyPath syntax的参考资料,如果您还没有阅读它们,那么还有很多内容。

上述绑定(两个绑定)相当于:

{Binding Path=/Date}

可以省略斜杠,如果在集合中找不到该属性,则绑定将查找当前项的属性。所以...

{Binding Date} binds to: CurrentItem -> Date
{Binding Count} binds to: Count

为清楚起见,我建议总是明确地写下斜杠。

将任何DataContext设置为{Binding}顺便提一下