简体   繁体   中英

UWP Visibility binding not working (mvvmlight)

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,

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM