我正在研究一个打卡时钟应用程序,作为学习C#,XAML和数据库的一种方式。现在,我的GUI并没有按照我的意愿行事。
我的每个按钮都有一个TextBlock
,就像Start Shift后显示的那样。
但是,当日期无效时,隐藏TextBlocks
。我已经能够显示和隐藏这些我想要的可见性。但是,当我跳过开始午餐和结束午餐并直接进入结束班次时,结束班次TextBlock
出现在开始午餐按钮之后,好像开始午餐和结束午餐TextBlocks
不同存在。
我在网上看过,发现可见性特征在隐藏时保留了空间,在折叠时没有预留空间,但是我使用的是隐藏的,它仍然没有保留空间。我的代码如下所示。
HomeView.xaml
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<!--Left column buttons-->
<StackPanel Grid.Column="0" Margin="10">
<Button Width="140" Content="Start Shift" Command="{Binding startShiftCommand}"
IsEnabled="{Binding startShiftActive}"/>
<Button Width="140" Content="Start Lunch" Command="{Binding startLunchCommand}"
IsEnabled="{Binding startLunchActive}"/>
<Button Width="140" Content="End Lunch" Command="{Binding endLunchCommand}"
IsEnabled="{Binding endLunchActive}"/>
<Button Width="140" Content="End Shift" Command="{Binding endShiftCommand}"
IsEnabled="{Binding endShiftActive}"/>
<Button Width="140" Content="Add Break" Command="{Binding addBreakCommand}"
IsEnabled="{Binding addBreakActive}"/>
</StackPanel>
<!--Right column text blocks-->
<StackPanel Grid.Column="1" Margin="10">
<TextBlock Text="{Binding day.timeIn, StringFormat=t, UpdateSourceTrigger=PropertyChanged, FallbackValue=StartTime}" FontSize="20"
Visibility="{Binding timeInValid, Converter={StaticResource BoolToVisConverter}}"
Padding="5" Margin="10"/>
<TextBlock Text="{Binding day.lunchStart, StringFormat=t, UpdateSourceTrigger=PropertyChanged, FallbackValue=LunchStart}" FontSize="20"
Visibility="{Binding lunchStartValid, Converter={StaticResource BoolToVisConverter}}"
Padding="5" Margin="10"/>
<TextBlock Text="{Binding day.lunchEnd, StringFormat=t, UpdateSourceTrigger=PropertyChanged}" FontSize="20"
Visibility="{Binding lunchEndValid, Converter={StaticResource BoolToVisConverter}, UpdateSourceTrigger=PropertyChanged}"
Padding="5" Margin="10"/>
<TextBlock Text="{Binding day.timeOut, StringFormat=t, UpdateSourceTrigger=PropertyChanged, FallbackValue=EndTime}" FontSize="20"
Visibility="{Binding timeOutValid, Converter={StaticResource BoolToVisConverter}, UpdateSourceTrigger=PropertyChanged}"
Padding="5" Margin="10"/>
</StackPanel>
</Grid>
BooleanToVisibilityConverter.cs
public class BooleanToVisiblityConverter : BaseValueConverter<BooleanToVisiblityConverter>
{
public override object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (parameter == null)
return (bool)value ? Visibility.Hidden : Visibility.Visible;
else
return (bool)value ? Visibility.Visible : Visibility.Hidden;
}
}
HomeViewModel.cs
private void startShift()
{
day.date = DateTime.Now;
dateValid = true;
day.timeIn = DateTime.Now;
timeInValid = true;
day.breaks = new ObservableCollection<Break>();
RaisePropertyChanged("day"); //A simple fix to update the UI
process(Command.startShift);
infoText = string.Format("Shift started at {0:t}", day.timeIn);
writeToFile();
}
private void startLunch()
{
day.lunchStart = DateTime.Now;
lunchStartValid = true;
RaisePropertyChanged("day");
process(Command.startLunch);
infoText = string.Format("Lunch started at {0:t}", day.lunchStart);
writeToFile();
}
private void endLunch()
{
day.lunchEnd= DateTime.Now;
lunchEndValid = true;
RaisePropertyChanged("day");
process(Command.endLunch);
infoText = string.Format("Lunch ended at {0:t}", day.lunchEnd);
writeToFile();
}
private void endShift()
{
day.timeOut = DateTime.Now;
timeOutValid = true;
RaisePropertyChanged("day");
calcTotalTime();
process(Command.endShift);
infoText = string.Format("Shift ended at {0:t}\nTotal time for {1:d} is {2}", day.timeOut, day.date, day.totalTime);
//Save to database
WorkHoursDBContext context = new WorkHoursDBContext();
context.Days.Add(day);
context.SaveChanges();
writeToFile();
}
方法process()
负责更改每个按钮的boolean
以决定是否启用。
当开始午餐和结束午餐TextBlock
被隐藏时,有什么方法可以阻止我的结束转换TextBlocks
向上滑动?