Reputation: 562
So I've tried all ways I know how to data bind, but I can't seem to get my property changed event to bind properly
I have a simple user control with the code behind being the following:
public partial class EnableForms : INotifyPropertyChanged
{
private GenericViewData _thisGenericViewData;
public GenericViewData ThisGenericViewData
{
get { return _thisGenericViewData; }
set
{
_thisGenericViewData = value;
OnPropertyChanged();
}
}
public EnableForms()
{
InitializeComponent();
//DataContext = this;
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
the view is the following XAML:
<UserControl x:Class="namespace.EnableForms"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"+
xmlns:local="clr-namespace:viewNamespace"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
DataContext="{Binding RelativeSource={RelativeSource self}}">
<!--d:DesignHeight="300" d:DesignWidth="300">-->
<Grid>
<TextBlock Text="{Binding Source=ThisGenericViewData}"></TextBlock>
<!-- <TextBlock Text="{Binding ThisGenericViewData, RelativeSource={RelativeSource AncestorType={x:Type local:EnableForms}}}" /> -->
</Grid>
using some old navigation logic I create the view and navigate to it thusly:
MainWindow.WindowControlHost.Navigate(new viewNamespace.EnableForms
{
ThisGenericViewData = viewData
});
I know the navigation logic works fine, and I can see that ThisGenericViewData is being set to valid data. My issue is that in my code behind, the propertychanged event is never set, it is always null.
I've tried in the code behind setting the datacontext to this (DataContext = this
) but that didn't work either. I've tried doing relative binding to self in the textblock but it doesn't work either. I know it is biding to the correct source because I can right click and go to source (when using the relative binding) and it navigates to the property.
Can someone please shed some light on the situation and show me what I'm doing wrong
Upvotes: 1
Views: 712
Reputation: 169370
You should set the Path (and not the Source) property of the Binding
to "ThisGenericViewData":
<TextBlock Text="{Binding Path=ThisGenericViewData}"></TextBlock>
This should work provided that you set the DataContext
of the UserControl to itself:
DataContext="{Binding RelativeSource={RelativeSource self}}"
The Path specifies the name of the property to bind to and the source specifies the source object where that property is defined.
Upvotes: 1
Reputation: 562
using this answer, the mention of element name the user thinks is a better way to do databinding to oneself. This is what worked for me. changing only the XAML it now looks like this
<UserControl x:Class="viewNamespace.EnableForms"
Name="EnableFormsView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
>
<!--d:DesignHeight="300" d:DesignWidth="300">-->
<Grid>
<TextBlock Text ="{Binding ThisGenericViewData, ElementName=EnableFormsView}" />
</Grid>
Upvotes: 0