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?