2017-04-18 66 views
1

我有兩個元素,一個WebBrowser和一個滑塊,我試圖隱藏一個和顯示另一個取決於用戶打開哪種類型的文檔,一個PDF或視頻文件(mp4)。但是,目前,當我打開PDF文件時,WebBrowser仍然處於隱藏狀態。WPF - 設置Web瀏覽器不工作的代碼背後的可見性

在MainWindow.xaml,我有

<WebBrowser x:Name="PDFView" Width="600" 
        Height="410" Visibility="{Binding PDFVisible}"> 
</WebBrowser> 

<FFmpegFramework:MediaElement Grid.Row="0" x:Name="MediaElement" 
        Height="400" Background="Gray" Visibility="{Binding VideoVisible}"/> 

在MainWindow.xaml.cs,我有

else if (fileName.Contains("pdf")) 
{ 

    MainWindowViewModel dataContext = this.DataContext as MainWindowViewModel; 
    dataContext.PDFVisible = Visibility.Visible; 
    dataContext.VideoVisible = Visibility.Collapsed; 

    //PDFView.Navigate("file:///" + fileName); 
} 
else if (fileName.Contains("mp4")) 
{ 
    MainWindowViewModel dataContext = this.DataContext as MainWindowViewModel; 
    dataContext.PDFVisible = Visibility.Collapsed; 
    dataContext.VideoVisible = Visibility.Visible; 
} 

在MainWindowModel.cs,我有:

public Visibility PDFVisible 
{ 
    get 
    { 
     return fPDFVisible; 
    } 
    set 
    { 
     this.SetProperty<Visibility>(ref fPDFVisible, value); 
    } 
} 

public Visibility VideoVisible 
{ 
    get 
    { 
     return fVideoVisible; 
    } 
    set 
    { 
     this.SetProperty<Visibility>(ref fVideoVisible, value); 
    } 
} 

private Visibility fPDFVisible = Visibility.Collapsed; 
private Visibility fVideoVisible = Visibility.Visible; 
+0

是否mainviewmodel執行INotifyPropertyChanged? – Krishna

+0

它實現了BindableBase,我剛剛查找並實現INotifyPropertyChanged –

+0

btw爲什麼要在xaml.cs中進行更改,而不是在視圖模型中進行更改? – Krishna

回答

0

你在.xaml.cs文件中執行此更改的操作比爲什麼要創建objects來更改visibility。您可以直接通過控制

if (fileName.Contains("pdf")) 
{ 
    PDFView.Visibility = Visibility.Visible; 
    MediaElement.Visibility = Visibility.Collapsed; 
} 
else if (fileName.Contains("mp4")) 
{ 
    PDFView.Visibility = Visibility.Collapsed; 
    MediaElement.Visibility = Visibility.Visible; 
} 

的名稱更改visibility不要忘記改變「模式=雙向」的結合。它也將改變財產ViewModel

<WebBrowser x:Name="PDFView" Width="600" 
      Height="410" Visibility="{Binding PDFVisible, Mode=TwoWay}" /> 

<FFmpegFramework:MediaElement Grid.Row="0" x:Name="MediaElement" 
       Height="400" Background="Gray" Visibility="{Binding VideoVisible, Mode=TwoWay}" /> 
相關問題