Can Not Bin to Dependency Property in Custom Control
-
Within my View Model i have a property and backer to bind to the ControlHideShow, I don't get a bind error but if I change the default value in the View Model nothing happens. If I don't bind in the Implementation XAML and set Visible or Hidden it works so why can I not bind to the ViewModel thanks Madaxe
Public interface ViewModel
{
Visibility ControlHideShow { get; set; }
}
public class MainPageViewModel : ViewModelBase, IMainPageViewModel
{
private Visibility _controlHideShow = Visibility.Visible;
public Visibility ControlHideShow
{
get => _controlHideShow;
set{ _controlHideShow = value; OnPropertyChanged(nameof(ControlHideShow)); }
}
}Implementation XML
ControlHideShow="{Binding ControlHideShow}"/>
Custom Control XAML
-
Within my View Model i have a property and backer to bind to the ControlHideShow, I don't get a bind error but if I change the default value in the View Model nothing happens. If I don't bind in the Implementation XAML and set Visible or Hidden it works so why can I not bind to the ViewModel thanks Madaxe
Public interface ViewModel
{
Visibility ControlHideShow { get; set; }
}
public class MainPageViewModel : ViewModelBase, IMainPageViewModel
{
private Visibility _controlHideShow = Visibility.Visible;
public Visibility ControlHideShow
{
get => _controlHideShow;
set{ _controlHideShow = value; OnPropertyChanged(nameof(ControlHideShow)); }
}
}Implementation XML
ControlHideShow="{Binding ControlHideShow}"/>
Custom Control XAML
I suspect it's because you've set the
DataContext
on the<UserControl>
element. That means the<controls:WaitTickerControl ... ControlHideShow="{Binding ControlHideShow}" />
is binding the user control's property to itself, rather than the property from the inherited viewmodel. Try removing theDataContext
assignment - you've specified theElementName
in your binding, so you shouldn't need it....
If that doesn't work, try setting the
DataContext
on theGrid
instead of theUserControl
.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
I suspect it's because you've set the
DataContext
on the<UserControl>
element. That means the<controls:WaitTickerControl ... ControlHideShow="{Binding ControlHideShow}" />
is binding the user control's property to itself, rather than the property from the inherited viewmodel. Try removing theDataContext
assignment - you've specified theElementName
in your binding, so you shouldn't need it....
If that doesn't work, try setting the
DataContext
on theGrid
instead of theUserControl
.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
Spot on, sometimes its the wood for the trees. thanks a bunch Madaxe