UWP Visibility binding not working (mvvmlight)
Asked Answered
L

1

5

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,

Limey answered 17/8, 2018 at 20:41 Comment(2)
Is the resource BoolToVisibilityConverter declared?Dit
Yes, it is. However with the x:bind syntax that would be unnecessaryLimey
L
7

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...

Limey answered 17/8, 2018 at 21:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.