I have two files -- the first (data) is an XML document, and the second (queries) contains XPath queries I would like to run on the first document.
I would like to populate a combobox in WPF to the queries, so that it displays the names of the XPath queries. When the user selects an item in the combobox, it should populate a listbox with selected nodes in the data document.
I have working code for the 2nd part (populating the listbox with a hard-coded XPath query), I just need to make it selectable. Here is the snippet of code I have working (minus the static resources, etc).
added on 1 Oct 2009
I've been staring at this for a bit, and I've broken down the problem into two key parts:
- I want the combobox to contain XPath queries that will populate the listbox (when I select a combobox item, it should update the listbox with valid entries from the XML document).
- I would like to load the queries for the combobox from a file. This way, users can add their own queries, and not have to recompile the application.
I've solved the first problem, and the code below is updated to reflect this.
<Grid Name="MainGrid" DataContext="{Binding Source={StaticResource xmldoc}}">
<ComboBox Height="23" Margin="0,1,0,0" Name="comboBox1" VerticalAlignment="Top">
<ComboBoxItem DataContext={Binding XPath=/find/something}">Query Name</ComboBoxItem>
<ComboBoxItem DataContext={Binding XPath=/find/something/else}">Other Query Name</ComboBoxItem>
</ComboBox>
<ListBox Height="100" Name="listBox1" VerticalAlignment="Top"
ItemsSource="{Binding ElementName=comboBox1, Path=SelectedItem.DataContext}"
ItemTemplate="{StaticResource listTemplate}" Margin="0,30,0,0" />
<TextBox Margin="0,136,0,29" DataContext="{Binding ElementName=listBox1, Path=SelectedItem}"
Text="{Binding XPath=.}" />
</Grid>
The key hear is the ItemsSource
for listBox1
: I originally tried to make Path=SelectedItem
(as I did for TextBox
), but it never worked. if you make the path the DataContext of the selected item, however, it works great.
This, of course, still leaves the second part: how do I load these queries from a separate file?