在打开对话框后更改WPF边框可见性

本文关键字:WPF 边框 可见性 打开对话框 | 更新日期: 2023-09-27 18:09:05

我有一个边界控件,当我打开几个大文件时,我使用它作为加载屏幕覆盖在主窗口上。为此,在创建对话框后,我将边界的可见性属性更改为可见。问题是边界从来没有真正显示出来。这是不能工作的代码:

  var openFileDialog = new ViewerOpenFileDialog();
  openFileDialog.ShowDialog();
  LoadingScreen.Visibility = Visibility.Visible;
  ViewerViewModel.OpenFile(openFileDialog.ParamFileName, openFileDialog.IdFileName);
  LoadingScreen.Visibility = Visibility.Hidden;

在我关闭对话框后,边框不再可见。

下面的代码可以正常工作:

   LoadingScreen.Visibility = Visibility.Visible;
   var openFileDialog = new ViewerOpenFileDialog();
   openFileDialog.ShowDialog();
   ViewerViewModel.OpenFile(openFileDialog.ParamFileName, openFileDialog.IdFileName);
   LoadingScreen.Visibility = Visibility.Hidden;

直到我的文件加载后边框才可见,但是当我的对话框打开时它是可见的,这是不理想的。

这是我的边框的XAML:

    <Border Name="LoadingScreen" Background="#80000000" VerticalAlignment="Stretch" Visibility="Hidden">
        <Grid>
            <TextBlock Margin="0" TextWrapping="Wrap" Text="Loading, Please Wait..." HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="30" FontWeight="Bold" />
        </Grid>
    </Border>

在打开对话框后更改WPF边框可见性

我假设,如果您关闭对话框,您的WPF表单需要呈现控件,因为OpenFileDialog覆盖了WPF窗口的一部分。如果你从CodeBehind设置可见性,你需要告诉你的窗口,它必须这样做再次渲染这个区域。

你可以试着调用:

LoadingScreen.Invalidate(true);


既然使用WPF,可能会有更好的解决方案。

期望你的第一个例子是在你的窗口的ViewModel中,你可以添加一个带有BackingField的属性,并实现INotifyPropertyChanged(当然也设置了DataContext):

private Visibility _loadScreenVisibility;
public Visibility LoadScreenVisibility
{
    get { return _loadScreenVisibility; }
    set
    {
        _loadScreenVisibility = value;
        OnPropertyChanged("LoadScreenVisibility");
    }
}
在XAML中,你可以使用
<Border Visibility="{Binding Path=LoadScreenVisibility, UpdateSourceTrigger=PropertyChanged}" ... >
    <... />
</Border>