Reputation: 14982
I am puzzled by a (I thought) simple thing to implement; make a UI element visible depending on a binding to a view model. I use the mvvmlight framework. When the binding (boolean) is set to true
the visibility binding does not react to the change.
XAML:
<Button
Command="{Binding NavigationCommand}" CommandParameter="{StaticResource Back}"
Visibility="{x:Bind (Visibility) ViewModel.ShowNavigationButtons}">
<Image Source="../../../Resources/NavigateBack.PNG"/>
</Button>
Code behind:
public sealed partial class MainPage
{
public MainPage()
{
InitializeComponent();
DataContext = new MainViewModel();
}
public MainViewModel ViewModel => DataContext as MainViewModel;
}
ViewModel:
public class MainViewModel : ViewModelBase
{
private bool _showNavigationButtons;
public RelayCommand BrakingCommand { get; }
public bool ShowNavigationButtons
{
get => _showNavigationButtons;
set { Set(() => ShowNavigationButtons, ref _showNavigationButtons, value); }
}
public MainViewModel()
{
BrakingCommand = new RelayCommand(() =>
{
ShowNavigationButtons = true;
NavigationCommand.RaiseCanExecuteChanged();
});
}
}
I also tried to bind "the WPF way" :
Visibility="{Binding ShowNavigationButtons, Converter{StaticResource BoolToVisibilityConverter}">
But that results in the exact same problem; the view doesn't react on the changed property.
Help is much appreciated,
Upvotes: 5
Views: 1417
Reputation: 14982
For the love of....
The problem was that the default mode for a binding is onetime
. Spend a freaking hour to figure that out. When I declare the binding as follows it works as expected...
Visibility="{x:Bind (Visibility) ViewModel.ShowNavigationButtons, Mode=OneWay}">
I hope that this helps somebody else one day who's pulling his hair out...
Upvotes: 7