rescueme
rescueme

Reputation: 69

wpf/silverlight binding help

Let's say i have a complex property called CarData and a simple bool AllowSubmit. Xaml:

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

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

The problem is that AllowSubmit is not binded. I think it's because the grid's DataContext is binded to CarData property, because if i put the button outside the grid it will work. Also i thought if i "override" DataContext by setting the Buttons DataContext to AllowSubmit will help but it won't work. I knew it's pretty a newbie question, but what is the proper way to go? I am pretty sure it's possible to bind the Button to a different property than the grid's property. Thank you for your help.

Upvotes: 2

Views: 70

Answers (1)

CodeNaked
CodeNaked

Reputation: 41393

The DataContext property is inherited by the controls inside it. So if you change it on the Grid, then it will effectively change it on all the controls inside also.

It sounds like you should be doing:

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

Here, the TextBox drills into the CarData so the Button's DataContext isn't affected.

Upvotes: 2

Related Questions