0

My XAML won't bind and I can't see why

Using MVVM, I have the following code in my MyView.xaml

<Expander Header="{Binding ThresholdName}" IsExpanded="False" >
    <properties:PropertiesVm Name="{Binding ThresholdName}" />
</Expander>

And this has a DataTemplate of MyViewModel.cs

As you can see, I'm using the ThresholdName twice. When I run my application, I can see the ThrehsoldName in the Header of the Expander indicating that the binding at this point is fine. The issue is binding to my "properties" object.

The Properties View is

<Grid>
    <TextBlock Text="{Binding Name}" />
</Grid>

And the ViewModel(which inherits from DependancyObject) is

    public static readonly DependencyProperty NameProperty = DependencyProperty.Register("Name", typeof(string), typeof(PropertiesVm));

    public string Name
    {
        get { return (Convert.ToString(GetValue(NameProperty))); }
        set { SetValue(NameProperty, value); }
    }

The error message I get is

System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=Time; DataItem=null; target element is 'PropertiesVm' (HashCode=6494098); target property is 'Name' (type 'String')

And within my Dictionary, I have set the DataTemplate as

<DataTemplate DataType="{x:Type viewModel:PropertiesVm}">
    <view:PropertiesView />
</DataTemplate>

This post suggests adding x:Name but that didn't help

Why does the property Name in my Properties ViewModel never get set? Or can I only do this with a UserControl, binding to code behind?

Community
  • 1
  • 1
MyDaftQuestions
  • 4,487
  • 17
  • 63
  • 120
  • http://stackoverflow.com/questions/29916756/custom-control-databinding-wpf/29917351#29917351 check this if you want use UserControls with Dependency Properties – blindmeis Oct 23 '15 at 13:29
  • @blindmeis, thanks for this, i'ts great information but I'm not setting the DataContext in my userControl, It inherits it naturally from the parent I thought – MyDaftQuestions Oct 23 '15 at 14:33
  • and @blindmeis, the same issue persists... – MyDaftQuestions Oct 23 '15 at 14:41
  • 1
    the binding within your usercontrol with a DP should always have ElemenName Binding or RelativSource Binding. i prefer the first one. i dont see this in your xaml. and pls use Snoop if you have binding problems. you can check on runtime your Datacontext. – blindmeis Oct 25 '15 at 15:52

1 Answers1

0

You've got things a bit about face.

Your Expander content needs to bind to a property of your top level view model which exposes your instance of PropertiesVm. A bit like:

public class TopLevelVm
{
  public string ThresholdName { get; }
  public PropertiesVm Properties { get; }
}

Then your XAML could be like:

<Expander Header="{Binding ThresholdName}" Content="{Binding }" />

Then the DataTemplate will pick the same DataContext, and your DataTemplate could look like this:

<Grid>
    <TextBlock Text="{Binding ThresholdName}" />
    <!-- Do something with .Properties -->
</Grid>
James Willock
  • 1,999
  • 14
  • 17