0

In a question I had posted before regarding databinding and UserControls, I was having problems getting simple properties set so that I could change colors, size, etc. Kent gave me some great pointers and that worked great. I then authored a new UserControl, and using his advice, had that working great as well.

Now I'm at the next step -- databinding commands. My current structure looks like this:

Window --contains--> UserControlB --contains--> UserControlA

Now databinding properties in UserControlA work great, and my UserControlB exposes these same properties so that the Window can change UserControlA indirectly. The problem is that UserControlB's DataContext is set something like this:

<UserControl x:Name="root">
  <Grid DataContext="{Binding ElementName=root}">
  ...
    <Button Command="{Binding MyCommand}" />
  ...
  </Grid>
</UserControl>

But I want MyCommand to be bound to my ViewModel. I thought it wass possible to set the DataContexts separately, but how do I get the Buttons to point to my ViewModel in XAML?

I found a related post, but didn't sound like what I want to do. I want to create the ViewModel in code, not in XAML.

Community
  • 1
  • 1
Dave
  • 14,618
  • 13
  • 91
  • 145

1 Answers1

1

Your binding should look something like this:

<Button Command="{Binding Path=DataContext.MyCommand, RelativeSource={RelativeSource     Mode=FindAncestor, AncestorType={x:Type TypeOfYourControlWithViewModelDataContext}}"/>
poindexter12
  • 1,775
  • 1
  • 14
  • 20
  • Thanks, I'll give it a try. I should just start experimenting with stuff, but I always get completely lost when trying to figure out how everything is connected, and what's a logical approach to setting Ancestors. – Dave Dec 06 '10 at 18:03
  • Hmm... no luck yet. I get this error: `System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='Company.MyPlugin', AncestorLevel='1''. BindingExpression:Path=DataContext.MyCommand; DataItem=null; target element is 'Button' (Name=''); target property is 'Command' (type 'ICommand')`. – Dave Dec 06 '10 at 18:15
  • It looks like you still don't have the datacontext right. You need to set the relative source type to whatever will have the datacontext of the viewmodel. also, you seem like you could get this automatically if you were not overriding the datacontext of your Grid to the the control, rather you could just allow the datacontext to naturally flow down from the parent. – poindexter12 Dec 06 '10 at 18:52
  • @poindexter: will not specifying the DataContext in the Grid allow my other UserControl (within this UserControl) to databind properly? I'll give it a try though. – Dave Dec 06 '10 at 19:12
  • @poindexter: interesting, so just removing the DataContext completely made everything work properly. I am very confused by this. I'll need to read more on the "flow" for databinding, but if you can post up any of your favorite articles, I'll gladly read them. :) Thanks for your advice. – Dave Dec 06 '10 at 19:18
  • sounds like you are just starting out, this is the best book to start with:http://www.amazon.com/gp/product/B00142KQES/ref=pd_lpo_k2_dp_sr_2?pf_rd_p=486539851&pf_rd_s=lpo-top-stripe-1&pf_rd_t=201&pf_rd_i=0596101139&pf_rd_m=ATVPDKIKX0DER&pf_rd_r=1B2W6H7JRPVETVRCHR8S – poindexter12 Dec 06 '10 at 19:21
  • in short, the datacontext is always set to that of its parent datacontext if you don't modify it. – poindexter12 Dec 06 '10 at 19:22
  • @poindexter: I'm embarrassed to say that I've been doing WPF for over a year now, but there are some gaps in my knowledge with databinding since I just learn enough to get by as I go through my projects. :) – Dave Dec 06 '10 at 21:49