2011-07-26 86 views
2

比方說,我有一個名爲CarData的複雜屬性和一個簡單的bool AllowSubmit。 的XAML:wpf/silverlight綁定幫助

<Grid ..... DataContext={Binding CarData}> 
...TextBoxes binded, working 
.... 

last line in the grid: 
<Button IsEnabled={Binding AllowSubmit}> 
</Grid> 

的問題是,AllowSubmit不綁定。我認爲這是因爲網格的DataContext綁定到CarData屬性,因爲如果我把按鈕放在網格外面,它會起作用。另外我想如果我通過將Buttons DataContext設置爲AllowSubmit「覆蓋」DataContext將有所幫助,但它不會工作。我知道這是一個新手問題,但要走的路是什麼?我很確定可以將Button綁定到與網格屬性不同的屬性。感謝您的幫助。

回答

2

DataContext屬性由其內部的控件繼承。所以如果你在網格中改變它,那麼它也會在內部的所有控件上有效地改變它。

這聽起來像你應該做的:

<Grid> 
    <TextBox Text="{Binding CarData.Make}" />> 
    <Button IsEnabled="{Binding AllowSubmit}" />> 
<Grid> 

這裏,文本框鑽入CarData使按鍵的DataContext的不受影響。

+0

非常感謝您的幫助。 – rescueme

+0

顯式設置DataContext屬性是一種不好的做法,除非您完全知道代碼將執行什麼操作。我同意CodeNaked解決方案。 –