簡體   English   中英

將TextBlock綁定到ComboBox值的.NextSibling

[英]Binding a TextBlock to the .NextSibling of a ComboBox Value

我有一個當前綁定到XML文件的ComboBox:

<ComboBox x:Name="employeeNameBox" IsReadOnly="False" HorizontalAlignment="Left" IsEditable="True" ItemsSource="{Binding Source={StaticResource People}, XPath=./Person/personName}" Text="Select Employee"/>

員工姓名正確地填充在組合框中。 我的下一步是嘗試將TextBlock綁定到所選員工姓名的同級節點之一:

<TextBlock x:Name="employeeEmail" DataContext="{Binding ElementName=employeeNameBox,Path=SelectedValue}" Text="{Binding XPath=./Person/personEmail}" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="0,5,0,0" MinWidth="120"/>

這是XML文件的結構:

<Person>
    <personName>John Doe</personName>
    <personEmail>someone@yahoo.com</personEmail>
    <personReports>Their reports here</personReports>
</Person>

根據組合框選擇的personName,我嘗試使TextBlock自動填寫相應的personEmail。 為了獲得更多的榮譽,我將要完成的下一步是將personReports填充到ListBox中:

<ListBox Height="100" Width="100" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="0,5,0,0" ItemsSource="{Binding ElementName=employeeNameBox,Path=SelectedItem.personReports}"/>

我感謝幫助人員,仍然學習與WPF綁定數據的所有細微差別。

謝謝!

默認情況下, SelectedValue包含當前所選項目的字符串,並且您不能將XPath與string一起用作DataContext

嘗試將DataContext綁定到SelectedItem而不是SelectedValue 然后,您可以使用以下XPath來獲取以下同級元素:

<TextBlock DataContext="{Binding ElementName=employeeNameBox, Path=SelectedItem}" 
            Text="{Binding XPath=./following-sibling::personEmail}"/>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM