2014-06-18 49 views
2

我有一個邊框控件,我用它作爲加載屏幕疊加在我的主窗口上,當我打開幾個大文件時。爲此,我在創建對話框後將邊框的可見性屬性更改爲Visible。問題是邊界從來沒有真正顯示出來。這是不起作用的代碼:打開對話框後更改WPF邊框的可見性

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

爲什麼不自定義忙指標的樣式模板? –

+0

您是否嘗試開始打開文件並將加載屏幕可見性設置爲隱藏的新線程?這應該給UI線程時間來使加載屏幕可見。 – Slade

回答

1

我認爲,如果你關閉對話框,您的WPF表單需要呈現控件, 因爲打開文件對話框覆蓋您的WPF窗口的部分。 如果你從CodeBehind設置可見性,你需要告訴你的窗口,它必須 再次渲染這個區域。

所以你可以嘗試撥打:

LoadingScreen.Invalidate(true);

在你的第一個例子設置可見之後。


由於您使用WPF,可能會有更好的解決方案。

期待您的第一個例子是在窗口的視圖模型,你可以只添加 一個屬性與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> 
+0

我通過使用您的建議將其設置在ViewModel中,並將文件讀入單獨的線程中,從而解決了這個問題。謝謝。 – wilk