2011-08-18 106 views
0

我有2個特性的視圖模型:綁定到CollectionViewSource在XAML

  • 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

在假設上述約束性指標的當前項目,這應該是等價的:

{Binding SomeCollectionViewSource.View/Date} 

另請參見Binding.Path和引用PropertyPath syntax如果您還沒有閱讀,有很多吧。

以上(你的兩個綁定)結合等同於:

{Binding Path=/Date} 

斜槓可以省略,如果屬性未在集合中發現的結合會查找當前項目的屬性。所以...

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

爲了清楚起見,我會建議總是寫明斜線。

設置任何DataContext{Binding}是順便真的毫無意義)

+0

感謝,我學到了很多來源於此。空的DataContect綁定只是爲了顯示不同之處。現在我只是缺少DatePicker上的IsReadOnly屬性。 – r03

+0

我不得不使用{Binding SomeCollectionViewSource.View/Date} – r03

+0

@Roeland:哦,對,綁定是直接的源,因爲它的目標是屬性路徑,如果CollectionViewSource被設置爲'Binding.Source'(或DataContext),綁定將隱含地定位到'View',所以在這些情況下,路徑只會是'/ Data'。 –