0

I'm sure I am doing something wrong.

Some context: WPF application, MVVM, Entity Framework, etc.

In my ViewModel for a window, I have two properties: MemberCount and EventCount. Both are int.

In my view, I have a 5 instances of a custom control. This custom control has three dependency Properties: ImageData, ButtonText, and ButtonSubText. All are string.

What I want to do is bind the control in such a way that MemberCount is bound to the ButtonText property on one control, and EventCount is Bound to ButtonText property on another control.

So I do something along the lines of:

<control:MyControl ... ButtonText="{Binding MemberCount, Mode=OneWay}" ButtonSubText="Members"  Background="{DynamicResource MetroRed}" />

The DataContext of my window is set to the correct ViewModel.

No matter what I try, I cannot get the member count or event count to show up on the control. I get an error in the output window:

    System.Windows.Data Error: 40 : BindingExpression path error: 'MemberCount' property
 not found on 'object' ''MyControl' (Name='button')'. BindingExpression:Path=MemberCount;
 DataItem='MyControl' (Name='button'); target element is 'MyControl' (Name='button');
 target property is 'ButtonText' (type 'String')

I am getting used to DataBinding in WPF, but for the life of me, I can't figure this one out.

What do I need to do to be able to bind a property from my view model to a property on my custom control?

STiLeTT
  • 1,023
  • 10
  • 23
DashTechnical
  • 442
  • 2
  • 13
  • 2
    The easiest way is to always check this with snoop, check if you have the correct DataContext on the controls where you try to bind to a property. The error says that Membercount was not found on MyControl, which means that you somewhere set the DataContext to an actual control instance, and not your ViewModel. – dowhilefor Aug 03 '12 at 15:28
  • Sure enough... I have no idea how or why, but the DataContext of the custom control was set to itself. I removed it and had to play with the dependency property on the control, but it works great now! Thanks for the quick help! – DashTechnical Aug 03 '12 at 15:59
  • **DataItem='MyControl'** –  Aug 04 '12 at 22:13

1 Answers1

0

i think the problem is the datacontext or binding within your usercontrol. you should post your usercontrol code.

<MyUserControl x:Name="uc">
  <TextBlock Text="{Binding ElementName=uc,Path=ButtonText}/>
</MyUserControl>

check my answer here, there is a similar problem.

Community
  • 1
  • 1
blindmeis
  • 22,175
  • 7
  • 55
  • 74