I have a ComboBox that is currently bound to an XML file:
<ComboBox x:Name="employeeNameBox" IsReadOnly="False" HorizontalAlignment="Left" IsEditable="True" ItemsSource="{Binding Source={StaticResource People}, XPath=./Person/personName}" Text="Select Employee"/>
The employee names populate inside the combobox correctly. The next step I have is trying to bind a TextBlock to one of the sibling node of the selected employee name:
<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"/>
Here is the structure of the XML file:
<Person>
<personName>John Doe</personName>
<personEmail>someone@yahoo.com</personEmail>
<personReports>Their reports here</personReports>
</Person>
Based on the personName selected by the ComboBox, I an trying to get the TextBlock to auto fill in the corresponding personEmail. For extra credit, the next step I will have is alos populating the personReports into a ListBox:
<ListBox Height="100" Width="100" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="0,5,0,0" ItemsSource="{Binding ElementName=employeeNameBox,Path=SelectedItem.personReports}"/>
I apprecite the help guys, still learning all the nuances to data binding a WPF.
Thanks!
By default, SelectedValue
contains string of currently selected item and you can't use XPath with string as DataContext
.
Try to bind DataContext
to SelectedItem
instead of SelectedValue
. Then you can use following XPath to get following sibling element :
<TextBlock DataContext="{Binding ElementName=employeeNameBox, Path=SelectedItem}"
Text="{Binding XPath=./following-sibling::personEmail}"/>
I have a ComboBox that is currently bound to an XML file:
<ComboBox x:Name="employeeNameBox" IsReadOnly="False" HorizontalAlignment="Left" IsEditable="True" ItemsSource="{Binding Source={StaticResource People}, XPath=./Person/personName}" Text="Select Employee"/>
The employee names populate inside the combobox correctly. The next step I have is trying to bind a TextBlock to one of the sibling node of the selected employee name:
<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"/>
Here is the structure of the XML file:
<Person>
<personName>John Doe</personName>
<personEmail>someone@yahoo.com</personEmail>
<personReports>Their reports here</personReports>
</Person>
Based on the personName selected by the ComboBox, I an trying to get the TextBlock to auto fill in the corresponding personEmail. For extra credit, the next step I will have is alos populating the personReports into a ListBox:
<ListBox Height="100" Width="100" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="0,5,0,0" ItemsSource="{Binding ElementName=employeeNameBox,Path=SelectedItem.personReports}"/>
I apprecite the help guys, still learning all the nuances to data binding a WPF.
Thanks!
By default, SelectedValue
contains string of currently selected item and you can't use XPath with string as DataContext
.
Try to bind DataContext
to SelectedItem
instead of SelectedValue
. Then you can use following XPath to get following sibling element :
<TextBlock DataContext="{Binding ElementName=employeeNameBox, Path=SelectedItem}"
Text="{Binding XPath=./following-sibling::personEmail}"/>
0 commentaires:
Enregistrer un commentaire