I am playing with the code for my Teched Hands on labs to dynamically rebind controls at run time and thought it might be worth breaking out separately. Particularly after the question about refreshing data in WPF

The XMAL below binds to set of potential answers(see image below) the code below that will rebind the listbox to only the correct Answer...One thing to watch for is the items collection for the Listbox is being done out of band so if you try something like look at the count it will incorrectly tell you 0.

 

 

<Window x:Class="Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <Window.Resources>
        <XmlDataProvider x:Key="QuestionDataProvider" XPath="Questions" Source="http://203.147.133.54/teched/questions.xml"/>
    </Window.Resources>
    <Grid>
        <ListBox  Background="{x:Null}" Foreground="#FF000000" Margin="7,74,4,71" Name="Listbox1" Opacity="1" BorderBrush="{x:Null}" >
            <ListBox.ItemsSource>
                <Binding Source="{StaticResource QuestionDataProvider}" XPath="//Question[@QID=2]/Answer"/>
            </ListBox.ItemsSource>
        </ListBox>
        <Button Height="44" Margin="99,0,96,13" Name="Button1" VerticalAlignment="Bottom">Button</Button>
    </Grid>
</Window>

 

image

 

 

Private Sub Button1_Click(

Dim mydata As New XmlDataProvider
mydata.Source = New Uri("http://203.147.133.54/teched/questions.xml")
mydata.XPath = "//Question[@QID=2]/Answer[@isCorrect='true']"
Listbox1.ItemsSource = mydata.Data
Dim mybinding As New Binding
mybinding.Source = mydata
BindingOperations.SetBinding(Listbox1, ItemsControl.ItemsSourceProperty, mybinding)

End Sub