2011-12-28 16 views
1

這個問題是一個跟進到一個我問及來到這裏回答:How to display XPS document using a selected combobox item如何鏈接ComboBoxItem到文件

我創建使用VB 2010年我通過XAML設置comboboxitems一個WPF應用程序。但是,我似乎無法弄清楚如何將每個項目的值設置爲文件路徑。

目的是讓用戶能夠從下拉列表中選擇一個項目,然後該選擇將在DocumentViewer中打開一個XPS文件。以下代碼由COMPETENT_TECH(謝謝)提供給我,用於閱讀並顯示DocumentViewer中所選組合框的值。

的路徑我要打開的文件是C:\文件夾\ file.xps

Private Sub Button4_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles Button4.Click 

    Try 
     Dim sFileName As String 

     sFileName = DirectCast(ComboBox1.SelectedValue, String) 
     If Not String.IsNullOrEmpty(sFileName) Then 
      Dim theDocument As New System.Windows.Xps.Packaging.XpsDocument(sFileName, System.IO.FileAccess.Read) 

      DocumentViewer1.Document = theDocument.GetFixedDocumentSequence() 
     End If 
    Catch ex As Exception 
     MessageBox.Show("ERROR: " & ex.Message) 
    End Try 


End Sub 

在此先感謝您的幫助。

更新

下面是我使用的XAML:

<ComboBox Width="Auto" IsReadOnly="True" IsEditable="True" Name="ComboBox1" Height="Auto" Margin="0" Padding="1" Grid.Column="2"> 
    <ComboBoxItem>123456</ComboBoxItem> 
    <ComboBoxItem>123457</ComboBoxItem> 
    <ComboBoxItem>123458</ComboBoxItem> 
</ComboBox> 
+0

? – 2011-12-28 09:22:03

+0

對不起,剛纔看到你的帖子。這裏是我使用的XAML - 沒有什麼奇怪的: 123456 123457 123458 2011-12-28 19:23:40

+0

好吧,是要從名爲類似於C文件:\ folder \ 123456.xps其中組合框中的值是沒有.xps的文件的名稱?還是有其他決定文件名的東西? – 2011-12-28 19:31:14

回答

1

取決於如何準確指定加載組合框的XAML,你可能會想改變這一行:

Dim theDocument As New System.Windows.Xps.Packaging.XpsDocument(sFileName, System.IO.FileAccess.Read) 

Dim theDocument As New System.Windows.Xps.Packaging.XpsDocument(System.IO.Path.Combine("C:\folder", sFileName & ".xps"), System.IO.FileAccess.Read) 

我們在新代碼中執行的所有操作都是將存儲文件的目錄與從組合框中檢索的文件名組合在一起。

更新

來檢索組合框的值的正確方法是:

If ComboBox1.SelectedValue IsNot Nothing Then 
    sFileName = DirectCast(ComboBox1.SelectedValue, ComboBoxItem).Content.ToString() 
End If 
+0

再次感謝您的幫助@competent_tech。對此,我真的非常感激。 因此,基於我發佈到您的請求的xaml,讓我解釋我想實現的目標。要查看的文件是由VIN#識別的車輛手冊。所以你在comboitem中看到的數字是VIN的最後6位 - 這將是文件名。但我不想將文件擴展名與文件名一起放置 - 那麼上面的答案將如何工作。我想我會得到你的答案,但這意味着我必須在組合框中列出完整的文件名,對吧? – 2011-12-28 19:29:29

+0

@KismetAgbasi:啊,現在我明白了!我已經更新了答案,將擴展名添加到組合框中的車輛標識符,這可以解決您的問題。 – 2011-12-28 19:33:17

+0

感謝老闆,看着你提供的代碼 - 我開始瞭解它背後的邏輯。但是,我現在得到的錯誤是這樣的: _無法將類型爲「System.Windows.Controls.ComboBoxItem」的對象轉換爲鍵入「System.String」。 這是否意味着VB無法將從comboboxitem派生的數字轉換爲字符串?如果是這樣,我該如何通過XAML設置組合數據項的數據類型(假設這將解決這個問題)? – 2011-12-28 19:45:03

0

我強烈建議你使用數據綁定組合框

創建一個類類似以下內容:

Class XPSDocumentInfo 
{ 
    Public Property Title As String 
    Public Property FileName As String 
} 

創建ObservableCollection(Of XPSDocumentInfo)並將其綁定到組合框的ItemsSource

使用DisplayMemberPath="Title"組合框所以它將使用標題屬性顯示在下拉菜單中,但既然你綁定型XPSDocumentInfo收集文本,的SelectedItem,返回的對象組合框 property屬性類型XPSDocumentInfo

例如,

sFileName = DirectCast(ComboBox1.SelectedValue, String) 

將你能告訴用於填充組合框的XAML的例子改爲

sFileName = DirectCast(ComboBox1.SelectedItem, XPSDocumentInfo).FileName 
+0

感謝您的建議。我使用VB,但我認爲你的代碼是在C#中。另外,我閱讀了MSDN網站上的Binding,希望能更好地理解如何使用此功能將xML文件綁定到我的組合框。不管怎麼說,還是要謝謝你。 – 2011-12-29 03:19:51