gpt4 book ai didi

c# - UI 不更新 bool 更新

转载 作者:行者123 更新时间:2023-12-03 10:33:04 25 4
gpt4 key购买 nike

我有一个矩形,它的可见性绑定(bind)到一个 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绑定(bind)到一个按钮,我想将矩形的可见性设置为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 (这是整个类(class)):
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
}

最佳答案

好的,我发现了问题,结果是我在 Button 上的这种风格对于 MouseEnterMouseLeave不知何故覆盖了矩形。这是风格,我将不得不重新考虑这一点:

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

关于c# - UI 不更新 bool 更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47457156/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com