UI没有更新bool更新

时间:2017-11-23 13:53:42

标签: c# wpf xaml mvvm

我有一个矩形,其可视性与bool绑定,并有一个BoolToVis转换器。这就是XAML中的样子:

<Rectangle Grid.Row="1" Fill="#FE7200" HorizontalAlignment="Left" Width="5" Visibility="{Binding LocationsClicked, Converter={StaticResource BoolToVis}, Mode=TwoWay}"/>

我有bool的get / set:

private bool _locationsClicked;
public bool LocationsClicked
{
    get => _locationsClicked;
    set
    {
        if (_locationsClicked == value) return;
        _locationsClicked = value;
        OnPropertyChanged(nameof(LocationsClicked));
    }
}

然后我有一个绑定到按钮的DelegateCommand,我想将矩形的可见性设置为true:

public DelegateCommand NavigateToLocationsCommand
{
    get
    {
        return new DelegateCommand(param =>
        {
            LocationsClicked = true;
        });
    }
}

通过调试器我遵循这一点,LocationsClicked设置为true但矩形不可见。我试过改变模式,奇怪的是如果我设置:

private bool _locationsClicked;

private bool _locationsClicked = true;

启动时可见矩形。我错在哪里,因为命令没有设置更新UI?

这就是我的转换器的样子:

<UserControl.Resources>
    <BooleanToVisibilityConverter x:Key="BoolToVis"/>
</UserControl.Resources>

有问题的ViewModel继承自BaseViewModel(这是整个班级):

internal class BaseViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged(string propertyName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

整个ViewModel:

internal class DashboardViewModel : BaseViewModel
{ 
    private object _selectedViewModel;

    private bool _jobsClicked = true;
    private bool _locationsClicked;
    private bool _clientsClicked;
    private bool _usersClicked;

    public DashboardViewModel()
    {
        SelectedViewModel = new JobsViewModel();
    }

    #region Get/Set
    public object SelectedViewModel
    {
        get => _selectedViewModel;
        set
        {
            if (_selectedViewModel == value) return;
            _selectedViewModel = value;
            OnPropertyChanged(nameof(SelectedViewModel));
        }
    }

    public bool JobsClicked
    {
        get => _jobsClicked;
        set
        {
            if (_jobsClicked == value) return;
            _jobsClicked = value;
            OnPropertyChanged(nameof(JobsClicked));
        }
    }

    public bool LocationsClicked
    {
        get => _locationsClicked;
        set
        {
            if (_locationsClicked == value) return;
            _locationsClicked = value;
            OnPropertyChanged(nameof(LocationsClicked));
        }
    }

    public bool ClientsClicked
    {
        get => _clientsClicked;
        set
        {
            if (_clientsClicked == value) return;
            _clientsClicked = value;
            OnPropertyChanged(nameof(ClientsClicked));
        }
    }

    public bool UsersClicked
    {
        get => _usersClicked;
        set
        {
            if (_usersClicked == value) return;
            _usersClicked = value;
            OnPropertyChanged(nameof(UsersClicked));
        }
    }
    #endregion

    private void ResetVisibility()
    {
        JobsClicked = false;
        LocationsClicked = true;
        ClientsClicked = false;
        UsersClicked = false;
    }

    #region Navigation
    public DelegateCommand NavigateToJobsCommand
    {
        get
        {
            return new DelegateCommand(param =>
            {
                SelectedViewModel = new JobsViewModel();
                ResetVisibility();
                JobsClicked = true;
            });
        }
    }

    public DelegateCommand NavigateToLocationsCommand
    {
        get
        {
            return new DelegateCommand(param =>
            {
                SelectedViewModel = new LocationsViewModel();
                ResetVisibility();
                LocationsClicked = true;
            });
        }
    }

    public DelegateCommand NavigateToClientsCommmand
    {
        get
        {
            return new DelegateCommand(param =>
            {
                SelectedViewModel = new ClientsViewModel();
                ResetVisibility();
                ClientsClicked = true;
            });
        }
    }

    public DelegateCommand NavigateToEmployeesCommand
    {
        get
        {
            return new DelegateCommand(param =>
            {
                SelectedViewModel = new UsersViewModel();
                ResetVisibility();
                UsersClicked = true;
            });
        }
    }
    #endregion
}

1 个答案:

答案 0 :(得分:1)

好的,我发现了这个问题,证明了Button MouseEnter MouseLeave上的这种风格,<EventTrigger RoutedEvent="MouseEnter"> <BeginStoryboard> <Storyboard> <ColorAnimation To="#FE7200" Storyboard.TargetProperty="(Button.Background).(SolidColorBrush.Color)" FillBehavior="HoldEnd" Duration="0:0:0.25"/> </Storyboard> </BeginStoryboard> </EventTrigger> <EventTrigger RoutedEvent="MouseLeave"> <BeginStoryboard> <Storyboard> <ColorAnimation To="#FF222226" Storyboard.TargetProperty="(Button.Background).(SolidColorBrush.Color)" FillBehavior="HoldEnd" Duration="0:0:0.25"/> </Storyboard> </BeginStoryboard> </EventTrigger> 以某种方式覆盖了矩形。这是风格,我将不得不重新考虑这个:

           <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-test</artifactId>
                <version>4.2.5.RELEASE</version>
                <scope>test</scope>
            </dependency>