Power BI

Content by Charles Sterling

Rebinding Controls in Windows Presentation Foundation

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″
    Title=”Window1″ Height=”300″ Width=”300″>
        <XmlDataProvider x:Key=”QuestionDataProvider” XPath=”Questions” Source=”“/>
        <ListBox  Background=”{x:Null}” Foreground=”#FF000000″ Margin=”7,74,4,71″ Name=”Listbox1″ Opacity=”1″ BorderBrush=”{x:Null}” >
                <Binding Source=”{StaticResource QuestionDataProvider}” XPath=”//Question[@QID=2]/Answer”/>
        <Button Height=”44″ Margin=”99,0,96,13″ Name=”Button1″ VerticalAlignment=”Bottom”>Button</Button>





Private Sub Button1_Click(

Dim mydata As New XmlDataProvider
mydata.Source = New Uri(“”)
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